0

I wrote a code linking my sql database with project. I have been attempting to create an ATM with graphics. i have the following tables in the database: table userinfo: accno(P key), name, balance table pin: accno(foreign key from userinfo), pin I wrote the following code, however on running it shows nullpointer exception which I am unable to fix. I tried to find the point where the exception occurs by writing system.out.println("world"); in order to check which part of the code is wrong. I found that my try block is not working and I am unable to fix this. Please help me. my code:

public void actionPerformed(ActionEvent evt) {
 try{
     System.out.println("world4");
 PreparedStatement ps=con.prepareStatement("Select * from pin where accno=? AND pin=?");
           System.out.println("world5");
  l1=Long.parseLong(acc);
        l2=Integer.parseInt(pin);

 ps.setLong(1,l1);
 ps.setInt(2,l2);
                System.out.println("world7");

 r=ps.executeQuery();
 PreparedStatement ps1=con.prepareStatement("Select * from pin where accno=?");
 ps1.setLong(1,Long.parseLong(tfnumber.getText()));
 r1=ps1.executeQuery();
 if(r.next())
 {
     m=new menu();
     setVisible(false);
     System.out.println("world1");
 }
 else if(r1.next())
         {   System.out.println("world2");
             lblerr=new Label("INVALID PIN!");
             lblerr.setBounds(470,400,80,30);
             img.add(lblerr);
             lblerr1.setVisible(false);
             lblerr.setVisible(true);
         }
 else
 {
     System.out.println("world3");
    lblerr1=new Label("ACCOUNT NOT FOUND");
     lblerr1.setBounds(370,300,80,30);
         img.add(lblerr1);
         lblerr.setVisible(false);
         lblerr1.setVisible(true);

  }

}
catch(Exception excp)
{
     System.out.println("world6");
     System.out.println(excp);
 }

here WORLD4,WORLD6 are printed on the screen, however, WORLD5&WORLD7 were not printed, indicating the non functional tryblock.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • And hint: you want us to spend our time to help you. So please: you spend some time to make that as easy as possible. That starts with well-formatted, human readable code. And just for the sake of it: your variable names are absolutely telling nothing. r1, ps1, r ... seriously: the days of 1 character name variables are like over since 20 years. Then: ever heard of *layering*? Like UI layer, application layer and database layer? The point is: you absolutely do not mix DB code and UI code. You really really want to read a good book, like "Agile principles" by Robert Martin... – GhostCat Jul 30 '16 at 21:18
  • i am new to all of this sir. Even the java coding. so i have no idea about layering. – Varunika Gupta Aug 06 '16 at 20:38
  • If you really are new to all of this: then it is a bad idea to start like this. Learning Java alone is a major task; UI and DB programming are advanced topics in themselves. I would try to tackle them **one by one**; not that the same time. And you know, I recommended that book so that you can start working on the basics. – GhostCat Aug 07 '16 at 14:19

0 Answers0