1

I am using eclipse with the JDTS driver. I am trying to insert a row into a MySQL database. The connection and statement are generated just fine (I think) However when I run my executeUpdate method, I get a null pointer exception.

I have a DBclient class which handles the connection to the DB, and then I have an Uploader class which will collect all the relevant information and supply it to my insert method.

My DBclient class:

public class DBclient {

String driver="net.sourceforge.jtds.jdbc";
String URL="xxxx";
String user="xxxx";
String pass="xxxx";
Connection connection;
Statement statement;
ResultSet rs;

public void connect() throws SQLException
{
    try {
        Class.forName(driver);
        }//try 
    catch (ClassNotFoundException e) 
        {
            e.printStackTrace();
        }//catch

    try {
        connection=DriverManager.getConnection(URL, user, pass);
        statement=connection.createStatement();

        } //try
    catch (SQLException e)
    {
        e.printStackTrace();
    }//catch
}//connect
public void insertToDB(String sqlstatement) throws SQLException
{
    int i=statement.executeUpdate(sqlstatement);
}

}//DBclient

and then my Uploader class

private String testinsert="insert into xxxx (Latitude, Longitude) values (1, 2)";


public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
     setContentView(R.layout.uploader);
     initialize();
     getaddress();
     update();
     try {
        client.insertToDB(testinsert);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        Toast t=Toast.makeText(getApplicationContext(), "oops", 4);
        t.show();
    }

   }//onCreate 
public void initialize()
{
    client = new DBclient();
    geocoder = new Geocoder(this);
    Bundle coords = getIntent().getExtras();
    street = (TextView) findViewById(R.id.street);
    town = (TextView) findViewById(R.id.town);
    state = (TextView) findViewById(R.id.state);
    country = (TextView) findViewById(R.id.country);
    lat=coords.getDouble("Latitude");
    lon=coords.getDouble("Longitude");

}//initialize
public void update()
{
    street.setText(address.getAddressLine(0));
    town.setText(address.getLocality());
    state.setText(address.getAdminArea());
    country.setText(address.getCountryName());
}//update
public void getaddress()
{
    try 
    {
        addresslist = geocoder.getFromLocation(lat, lon, 1);
    } //try
    catch (IOException e) 
    {
        Toast t=Toast.makeText(getApplicationContext(), "Input Error", 2);
        t.show();
    }//catch
     address=addresslist.get(0);

}

}//Uploader

The Null pointer exception is thrown from the insertToDB() method from the DBclient class.

Any help will be appreciated.

0nyx
  • 144
  • 1
  • 11

2 Answers2

2
public void insertToDB(String sqlstatement) throws SQLException
{
    int i=statement.executeUpdate(sqlstatement);
}

It looks like statement is only ever set in connect() in the DBClient class, but this method isn't called in any of the other code shown in the question. Therefore statement will be null, and calling methods from it will result in the NullPointerException.

raveturned
  • 2,440
  • 24
  • 29
  • You are correct. However upon fixing the issue, I still get the same NullPointerException. I created a new project to test for any connectivity issues, and the identical code works. I copied and pasted and made no changes except that the new class is called tester. Do you think it has to do with the original being an android project? DBclient is a standard Java class being instantiated into a class extending Activity. In DBclient, it won't run unless it is surrounded with a try catch clause. That is the only difference I can think of, – 0nyx May 25 '12 at 15:07
  • Also I am aware that the driver string was incorrect. It has been changed to "net.sourceforge.jtds.jdbc.Driver" and works fine in the test project. – 0nyx May 25 '12 at 15:10
  • @BrockHallenbeck If you're still getting the NPE at the same place it implies that `statement` is still null. Perhaps try adding logging/debug code to `connect()` inside the `try` block after `statement` has been assigned to ensure it isn't throwing an exception earlier. Depending on the settings of your Android debug environment, you may not see the output of e.printStackTrace() if it's being called. – raveturned May 25 '12 at 15:32
  • 1
    I modified connect() to return a string. "success" if connection!=null, else "failure". It returned failure. So now we know that the connection is not even taking place. I am totally baffled because the identical code in the test project works fine. We thought it might be a firewall issue as the android is on a public wifi network and the test project is not, however I just ran it through eclipse's emulator which is on the same network as the test program, and still got "failure". I guess I have some googling to do. If you manage to find anything before me it will be greatly appreciated. – 0nyx May 25 '12 at 15:50
  • I think I have found the problem. I am getting a NoClassDefFoundError and a ClassNotFoundException on "net.sourceforge.jtds.jdbc.Driver" when trying to get the connection. If I researched correctly, I need to add the jtds jar file to my classpath. The thing that is killing me is that I thought that I already did. I dragged the jar file into my project, and then right clicked, add to build path."net.sourceforge.jtds.jdbc" is now under the jar file in Referenced Libraries. Do I have to do something with my Manifest.xml? I am having difficulty finding any relevant hits on Google. – 0nyx May 25 '12 at 17:36
0

It turns out that I was having the same issue as the user here

What I had to do was add the jtds.jar to my assetts folder, add it to my build path, and then in the "Configure Build Path" menu, go to order and export, and check the jar file.

Community
  • 1
  • 1
0nyx
  • 144
  • 1
  • 11