2012-05-25 70 views
1

我正在使用eclipse和JDTS驱动程序。我正在尝试向MySQL数据库中插入一行。连接和语句生成就好(我认为)但是,当我运行我的executeUpdate方法时,我得到一个空指针异常。将行插入MySQL数据库

我有一个DBclient类,它处理到数据库的连接,然后我有一个Uploader类,它将收集所有相关信息并将其提供给我的插入方法。

我与dbclient类:

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中

,然后我上传类

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); 

} 

} //上传

的空指针异常从抛出来自DBclient类的insertToDB()方法。

任何帮助将不胜感激。

+0

此外,运行“testinsert”sql语句在SQL资源管理器中工作。 – 0nyx

回答

0

事实证明,我也有同样的问题,因为用户here

我有什么做的是在jtds.jar添加到我的assetts文件夹,将其添加到我的构建路径,然后在“配置构建路径“菜单,去定购并导出,并检查jar文件。

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

它看起来像statement只能用在connect()DBClient类设置,但这种方法没有在任何的问题显示的其他代码的调用。因此statement将为空,并从中调用方法将导致NullPointerException

+0

你是对的。然而,在解决问题后,我仍然得到相同的NullPointerException。我创建了一个新项目来测试任何连接问题,并且相同的代码有效。我复制并粘贴,并没有改变,除了新的类被称为测试者。你认为它与原始的android项目有关吗? DBclient是一个标准的Java类,被实例化为一个扩展Activity的类。在DBclient中,除非它被try catch子句包围,否则它不会运行。这是我能想到的唯一区别, – 0nyx

+0

另外我知道驱动程序字符串不正确。它已更改为“net.sourceforge.jtds.jdbc.Driver”,并在测试项目中正常工作。 – 0nyx

+0

@BrockHallenbeck如果你仍然在同一个地方得到NPE,它暗示'语句'仍然是空的。也许可以在'statement'已经分配之后尝试在'try'块内部将日志/调试代码添加到'connect()',以确保它没有在之前抛出异常。根据Android调试环境的设置,如果正在调用e.printStackTrace(),您可能看不到输出。 – raveturned