url连接中的java.lang.nullpointerexception

时间:2013-09-12 09:44:21

标签: java

我是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 个答案:

答案 0 :(得分: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>"));   
}

答案 1 :(得分:0)

getResponseFromUrl方法中,String输入是一个新变量; 您必须将String input = inputLine;更改为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();
        }
    }   
}

答案 2 :(得分: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>"));   

答案 3 :(得分:0)

我可能错了......但你将String输入定义为你班级的一个字段...... 我相信你想在public void getResponseFromUrl()中使用String输入字段而不是本地输入!!

为什么我这么说......显然你在public 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...
    ...
}