我試圖做一些mysql表插入操作,但我發現這個java.lang.NullPointerException當我輸入來自我的小程序,couldnt弄清楚我在這段代碼中錯了什麼地方可以你可以幫我!我試着做數據庫操作,而小程序與代碼此相同的部分,它的工作運行我的小程序時發生java.lang.NullPointerException
import java.applet.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;
public class StudentEntry extends Applet implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
Label l1,l2,l3,l4;
TextField t1,t2,t3,t4;
Button b1;
public void init(){
Label l1=new Label("Enter your Name : ");
add(l1);
TextField t1=new TextField(20);
add(t1);
Label l2=new Label("Enter your Sem : ");
add(l2);
TextField t2=new TextField(20);
add(t2);
Label l3=new Label("Enter your Branch : ");
add(l3);
TextField t3=new TextField(20);
add(t3);
Label l4=new Label("Enter your Contact Number : ");
add(l4);
TextField t4=new TextField(20);
add(t4);
Button b1=new Button("Submit");
add(b1);
b1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
try
{
String sname=t1.getText();
String semT=t2.getText();
int sem=Integer.parseInt(semT);
String branch=t3.getText();
String contact=t4.getText();
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Mice","root","1234");
Statement stmt=con.createStatement();
String Uquery="Insert into StudentDetails(student_name,sem,branch,student_con)values('"+sname+"',"+sem+",'"+branch+"','"+contact+"')";
int val=stmt.executeUpdate(Uquery);
if(val==1)
{
System.out.println("succesfull inserted");
}
ResultSet rs=stmt.executeQuery("select * from StudentDetails");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getInt(3)+" "+rs.getString(4)+" "+rs.getString(5));
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
//代碼結束
你在哪一行得到NPE? – bish
同時收集來自文本字段的輸入在actionPerformed – Hunt