0

I get error on

java.lang.NullPointerException: Attempt to invoke interface method 'java.sql.Statement java.sql.Connection.createStatement()' on a null object reference at com.example.jacquesarroyo.onlinelibrary.suggestions$3.onClick(suggestions.java:81)

Here's the code I used:

add = (Button) findViewById(R.id.btnadd);
errorlbl = (TextView) findViewById(R.id.lblerror);
title = (EditText) findViewById(R.id.txttitle);
author = (EditText) findViewById(R.id.txtauthor);
year = (EditText) findViewById(R.id.txtyear);
location = (EditText) findViewById(R.id.txtlocation);

ipaddress = "10.0.0.5";   
db = "SampleDatabase"; 
username = "admin"
password = "admin";
connect = ConnectionHelper(username, password, db, ipaddress);

add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                st = connect.createStatement();
                preparedStatement = connect
                        .prepareStatement("Insert into Books(Title,Author,Year) values ('"
                                + title.getText().toString()
                                + "','"
                                + author.getText().toString()
                                + "','"
                                + year.getText().toString() + "')");
                preparedStatement.executeUpdate();
                errorlbl.setText("Data Added successfully");
            } catch (SQLException e) {
                errorlbl.setText(e.getMessage().toString());
            }
        }
    });

@SuppressLint("NewApi")
private Connection ConnectionHelper(String user, String password,
                                    String database, String server) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    Connection connection = null;
    String ConnectionURL = null;
    try {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        ConnectionURL = "jdbc:jtds:sqlserver://" + server + ";"
                + "databaseName=" + database + ";user=" + user
                + ";password=" + password + ";" + "ssl=false";
        connection = DriverManager.getConnection(ConnectionURL);


    } catch (SQLException se) {
        Log.e("ERROR", se.getMessage());
    } catch (ClassNotFoundException e) {
        Log.e("ERROR", e.getMessage());
    } catch (Exception e) {
        Log.e("ERROR", e.getMessage());
    }
    return connection;
}

line 81 is: st = connect.createStatement();

George
  • 6,886
  • 3
  • 44
  • 56
jaxarroyo
  • 190
  • 2
  • 15
  • 3
    Since there is no `createStatement()` call, or an `onClick()` method, in the code in your question, we cannot really help you much. You might consider posting the code that is cited in your error. – CommonsWare Aug 23 '15 at 23:41
  • 1
    The error is on file "suggestions.java", line 81. Which of these is line 81? – George Aug 23 '15 at 23:41
  • line 81 is in: st = connect.createStatement(); – jaxarroyo Aug 23 '15 at 23:46
  • 1
    put on the rest of the code. thanks – jaxarroyo Aug 23 '15 at 23:50
  • 1
    You must be getting exceptions in the log from the `ConnectionHelper` method, because it seems to be returning a `null` reference. Perhaps you're using an incorrect URL. Also, method and variable names should start with a lower case letter in Java (`private Connection connectionHelper(...)`, `String connectionURL` etc.). – Mick Mnemonic Aug 23 '15 at 23:55

1 Answers1

2

NullPointerException means that the variable you are trying to use is not initialized or is null.

Before calling:

connect.createStatement();

you have to initialize the connect object first by:

Connection connect = ConnectionHelper(user,password,database,server);

Keep in mind scope of the variables.

itwasntme
  • 1,442
  • 4
  • 21
  • 28