1

I Have Looke over the internet that from Last 12 hours , and found that too many Users are facing that Problem , But None of them are Able to get Rid of That, I hav Created a JDialog , Which have TextFields, and i am Trying to get Input From those text Fields, and Store That In DataBase, But is Givi the Following Exception.

Exception occurred during event dispatching:
Connection ok
Adnan
java.lang.NullPointerException
at srvrDataBaseClass.setPersonStatement(srvrDataBaseClass.java:90)
at srvrDataBaseClass.insertPerson(srvrDataBaseClass.java:71)
at EnrollmentForm.setPerson(EnrollmentForm.java:90)

Here is the Code Where the StackTrac is Pointing ,

public void setPersonStatement(String nm,String fn,String cn,String add, byte[] fpt) {
    String Sql = "INSERT INTO PERSON (NAME, FNAME, CNIC, ADDR, FPT) VALUES ( ?,?,?,?,?)";
    try {
        if(con==null){
            System.out.println("Connection error");   <---------------- Connection is Not Closed
        }
        else {
            System.out.println("Connection ok");    <------Connection ok
        }


        con.prepareStatement(Sql);

    System.out.println(nm);     <----- This is Line :90,  But You can see its not Null, as the Value 'Adnan' is printed on cmd,
    pst2.setString(1, nm);
    pst2.setString(2, fn);
    pst2.setString(3, cn);
    pst2.setString(4, add);
    pst2.setBytes(5, fpt);

    pst2.executeUpdate();

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        System.out.println("SQL Error");
        e.printStackTrace();
    }
    }

So Any One Who can tell me , Whats Wrong with my Code

Note: DataBase is SQL Server 2008,

2
  • That line cannot (well it could, but i don't see you changing System.out) throw a NullPointerException. Commented Jan 29, 2014 at 2:26
  • 1
    exactly; this line is wrong: con.prepareStatement(Sql); not assigned to anything Commented Jan 29, 2014 at 2:30

2 Answers 2

5

Did you mean to do

pst2 = con.prepareStatement(Sql);

? It seems like pst2 is null and will remain so in

pst2.setString(1, nm);

which probably actually throws the NPE.

Check out the javadoc for the method you are trying to use.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, And Now i got another Error Message. com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would be truncated. at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216) at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
@AdnanAhmadKhan That's a different problem altogether. You would probably be better off looking for related questions or asking a new one with all the details provided. Check your table schema. Does the byte[] make sense as something you would enter into that column?
1
con.prepareStatement(Sql);

is not assigned to a proper PreparedStatement Variable,

write as

pst2= con.prepareStatement(Sql);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.