1

我是java编程的初学者。我正在尝试从 url 读取响应并插入数据库。我运行了下面发布的程序,它返回了这个错误:

Exception in thread "main" java.lang.NullPointerException
    at javaapp.JavaApplication1.parseResponseString(JavaApplication1.java:41)
    at javaapp.JavaApplication1.main(JavaApplication1.java:71)

我一直试图通过研究自己解决这个问题,但我无法解决。我想知道是否有人可以帮助我。如果您确实需要有关我的程序的任何其他信息,请询问。提前致谢!

这是我写的代码

enter code here 包javaapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class JavaApplication1 
{

Map<String,String> responseMap=new HashMap<String, String>();
    static String input;

    public void getResponseFromUrl() throws IOException
    {
        URL url = new URL("http://localhost:8084/home/Home");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())) ;
       String inputLine;
        while ((inputLine = in.readLine()) != null) 
        {
           if (inputLine.contains("<h1>")) 
            {
                String input = inputLine;
                input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));   
            }
        }

    }
    public void parseResponseString(String input)
    {
        String params[]=input.split(",");

        for(String param:params)
        {
            String key= param.substring(0,param.indexOf('='));
            String value= param.substring(param.indexOf('=')+1,param.length());
            responseMap.put(key, value);
            System.out.println(param);
        }
    }
    public void insertToDatabase() throws SQLException, ClassNotFoundException
    {
        Connection conn=null;
        Class.forName("oracle.jdbc.driver.OracleDriver");

        conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jai","jaikiran");
        String insertQuery = " INSERT INTO value_1 (username1,username2,username3,username4,username5) "+
                             " values (?,?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(insertQuery);
        pstmt.setString(1,responseMap.get("username1"));
        pstmt.setString(2,responseMap.get("username2"));
        pstmt.setString(3,responseMap.get("username3"));
        pstmt.setString(4,responseMap.get("username4"));
        pstmt.setString(5,responseMap.get("username5"));
        pstmt.executeUpdate();
    }
    public static void main(String[] args) throws MalformedURLException, IOException, SQLException, ClassNotFoundException 
    {
        JavaApplication1 application =new JavaApplication1();
        application.getResponseFromUrl();
        application.parseResponseString(input);//shows exception here
        try {
            application.insertToDatabase();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   
}





Thanks in advance,please helpme..
4

4 回答 4

0

static String input;默认情况null下,您将其传递给方法parseResponseString()

您需要为input变量赋值。

getResponseFromUrl()我相信您希望在创建新input变量的方法中完成分配。为了static String input;分配一个值,您需要使用this关键字来引用它。

if (inputLine.contains("<h1>")) 
{
    String input = inputLine;
    this.input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));   
}
于 2013-09-12T09:46:50.433 回答
0

getResponseFromUrl方法中,字符串输入是一个新变量;你必须改变 String input = inputLine;to String input2 = inputLine;input = input2.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));

你的代码变成:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class JavaApplication1 
{

Map<String,String> responseMap=new HashMap<String, String>();
    static String input;

    public void getResponseFromUrl() throws IOException
    {
        URL url = new URL("http://localhost:8084/home/Home");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())) ;
       String inputLine;
        while ((inputLine = in.readLine()) != null) 
        {
           if (inputLine.contains("<h1>")) 
            {
                String input2 = inputLine;
                input = input2.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));   
            }
        }

    }
    public void parseResponseString(String input)
    {
        String params[]=input.split(",");

        for(String param:params)
        {
            String key= param.substring(0,param.indexOf('='));
            String value= param.substring(param.indexOf('=')+1,param.length());
            responseMap.put(key, value);
            System.out.println(param);
        }
    }
    public void insertToDatabase() throws SQLException, ClassNotFoundException
    {
        Connection conn=null;
        Class.forName("oracle.jdbc.driver.OracleDriver");

        conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","jai","jaikiran");
        String insertQuery = " INSERT INTO value_1 (username1,username2,username3,username4,username5) "+
                             " values (?,?,?,?,?)";
        PreparedStatement pstmt = conn.prepareStatement(insertQuery);
        pstmt.setString(1,responseMap.get("username1"));
        pstmt.setString(2,responseMap.get("username2"));
        pstmt.setString(3,responseMap.get("username3"));
        pstmt.setString(4,responseMap.get("username4"));
        pstmt.setString(5,responseMap.get("username5"));
        pstmt.executeUpdate();
    }
    public static void main(String[] args) throws MalformedURLException, IOException, SQLException, ClassNotFoundException 
    {
        JavaApplication1 application =new JavaApplication1();
        application.getResponseFromUrl();
        application.parseResponseString(input);//shows exception here
        try {
            application.insertToDatabase();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   
}
于 2013-09-12T09:52:21.920 回答
0

我认为你的问题在这里:

String input = inputLine;
input = input.substring(input.indexOf("<h1>") + 4, input.indexOf("</h1>"));   

在第一行声明一个新变量input会阻止您的代码将值分配给私有字段input(这可能是您尝试做的)。另外,第一行没用,去掉直接inputLine在第二行使用即可:

input = inputLine.substring(inputLine.indexOf("<h1>") + 4, inputLine.indexOf("</h1>"));   
于 2013-09-12T09:56:16.910 回答
0

我可能是错的...但是您将字符串输入定义为类的字段...我相信您想在 public void getResponseFromUrl() 中使用字符串输入字段而不是本地输入!

为什么我这么说...显然您尝试在公共 void parseResponseString(String input) 中获取输入字段中的数据,但您那里没有任何内容...所以您得到 NullPointerException。

所以你需要改变的是:

public void getResponseFromUrl() throws IOException
{
    .....
    /*String input = inputLine; -> wrong, because you want to use a field and not a local var... */
    input = inputLine; /* now you are using the class field input */
    input = input.substring...
    ...
}
于 2013-09-12T10:06:18.110 回答