-2

It says that I'm receiving the error at line 506 which is the one saying

firstTxt.setText(newPerson.getFirstname());

Here is the first two lines of error code: Exception in thread

AWT-EventQueue-0" java.lang.NullPointerException at 
    dataawaredatajava.JFrame.addButtonActionPerformed(JFrame.java:506) 

This only happens whenever I click on the "Add?" button or addButton. I use a similar method of casting my object with my update button, but that seems to work perfectly fine!

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    switch (addButton.getText()) {
        case "Add New Person":
            lstPerson.clearSelection();
            blockForAddOrUpdate(true);
            addButton.setEnabled(true);
            addButton.setText("Add?");
            break;
        case "Add?":
            Person newPerson = (Person)lstPerson.getSelectedValue();
            firstTxt.setText(newPerson.getFirstname());
            lastTxt.setText(newPerson.getLastname());
            dobTxt.setText(newPerson.getDob());
            occupationTxt.setText(newPerson.getOccupation());
            marriedChB.setSelected(newPerson.getMarried());
            noOfKidsJSp.setValue(newPerson.getNumberofkids());
            dataAwareDataJavaPUEntityManager.getTransaction().begin();
            dataAwareDataJavaPUEntityManager.persist(newPerson);
            dataAwareDataJavaPUEntityManager.getTransaction().commit();
            addButton.setText("Add New Person");
            blockForAddOrUpdate(false);
            personList.add(newPerson);
            lstPerson.setSelectedValue(newPerson, true);
            break;
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    It appears that either `firstTxt` or `newPerson` is `null`. Find out which one to identify your problem. – carloabelli Jul 29 '14 at 16:22
  • The NPE means that either firstTxt or newPerson on the line where you try to use them. Find out and fix it. More importantly, you need to learn the general concepts of how to debug a NPE (NullPointerException). You should inspect the line carefully that throws it, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me. You will also want to work on your search skills as this question is extremely common here and on any other Java advice site you'll find. – Hovercraft Full Of Eels Jul 29 '14 at 16:22
  • NullPointerException is just about the easiest of all Java exceptions to debug. Hone up your debugging skills. – Hot Licks Jul 29 '14 at 16:23
  • Busy debugging as we type. I'll let you guys know what comes of it. – Ryan de Kwaadsteniet Jul 29 '14 at 20:01

2 Answers2

0

Either there is no selection in lstPerson or firstTxt was never instantiated.

Rocky Pulley
  • 22,531
  • 20
  • 68
  • 106
0

It appears that either firstTxt or newPerson is null when you call that line. Find out which one to identify your problem. You could do this with a simple System.out.print.

carloabelli
  • 4,289
  • 3
  • 43
  • 70