1

I am trying to get the data from input panel to be transferred by the submit button to output panel. Even though I manage to go so far without error, I am unsure why I cannot add the text labels to read the input from those fields without getting the error below. The uname labels are the one causing trouble at main screen. But I don't know any other areas to add to show the output. By the way, I am beginner in java.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
   at testabstract.Mainsystem$1.formEventOccurred(Mainsystem.java:53)
   at testabstract.Inputpanel$1.actionPerformed(Inputpanel.java:146)

My main screen code(error at the labels)

       inputPanel.setFormListener(new FormListener(){

              public void formEventOccurred(InputEvent e)
              {
                  String name = e.getName();
                  String passport = e.getPassport();
                  String contact = e.getContact();
                  String email = e.getEmail();
                  String touristnum = e.getTouristnum();


                  unamelbl.setText(name);
                  upassportlbl.setText(passport);
                  ucontactlbl.setText(contact);
                  uemaillbl.setText(email);
                  utouristnumlbl.setText(touristnum);

              }
       });

Input fields panel

Submitbtn.addActionListener(new ActionListener()
       {
              public void actionPerformed(ActionEvent e)
              {
               String name = namef.getText();
               String passport = passportf.getText();
               String contact = contactf.getText();
               String email = emailf.getText();
               String touristnum = touristnumf.getText();

               String season = Seasons.getSelectedItem().toString();
               //useasonlbl.setText(season);
               String region = Regions.getSelectedItem().toString();
               //uregionlbl.setText(region);

               String meal = Meals.getSelectedItem().toString();
               //umeallbl.setText(meal);

               InputEvent ev = new InputEvent(this, name, passport, contact, email, touristnum);
               }
               if(formListener !=null)
               {
                   formListener.formEventOccurred(ev);
               }
              }
       });

                  public void setFormListener(FormListener listener){       
                  this.formListener = listener;
                  } 

Input Event collector

public class InputEvent extends EventObject{

    private String name;
    private String passport;
    private String contact;
    private String email;
    private String touristnum;
    //private String name;
    //private String name;
    //private String name;


    public InputEvent(Object source) {
        super(source);
    }

    public InputEvent(Object source, String name, String passport, String contact, String email, String touristnum) {
        super(source);

        this.name = name;
        this.passport = passport;
        this.contact = contact;
        this.contact = email;
        this.contact = touristnum;

    }

    public String getName()
    {
       return name;
    }

    public void setName(String name)
    {
       this.name = name;
    }

    public String getPassport()
    {
       return passport;
    }

    public void setPassport(String passport)
    {
       this.passport = passport;
    }

    public String getContact()
    {
       return contact;
    }

    public void setContact(String contact)
    {
       this.contact = contact;
    }

    public String getEmail()
    {
       return email;
    }

    public void setEmail(String email)
    {
       this.email = email;
    }

    public String getTouristnum()
    {
       return touristnum;
    }

    public void setTouristnum(String touristnum)
    {
       this.touristnum = touristnum;
    }

Form listener which take notes of events

public interface FormListener extends EventListener{

    public void formEventOccurred(InputEvent e);


}

Output Panel

public Outputpanel()
    {
      //creating the labels beside output
       rnamelbl = new JLabel("Full Name: ");
       rpassportlbl = new JLabel("Passport: ");
       rcontactlbl = new JLabel("Contact No: ");
       remaillbl = new JLabel("Email: ");
       rtouristnumlbl = new JLabel("Tourist No: ");
       rseasonlbl = new JLabel("Season: ");
       rregionlbl = new JLabel("Region: ");
       rmeallbl = new JLabel("Meal: ");

       //extra label for output
       travelcostlbl = new JLabel("Travel Cost: ");
       tourguidelbl = new JLabel("Tour Guide: ");

       //creating the labels for output
       unamelbl = new JLabel();
       upassportlbl = new JLabel();
       ucontactlbl = new JLabel();
       uemaillbl = new JLabel();
       utouristnumlbl = new JLabel();
       useasonlbl = new JLabel();
       uregionlbl = new JLabel();
       umeallbl = new JLabel();
       utravelcostlbl = new JLabel();
       utourguidelbl = new JLabel();

       JPanel ResultPan = new JPanel(new GridBagLayout());
       add(ResultPan, BorderLayout.WEST);
       ResultPan.setBorder(BorderFactory.createTitledBorder("Receipt"));
         //label constraints
       GridBagConstraints lblC = new GridBagConstraints();
       lblC.anchor = GridBagConstraints.WEST;
       lblC.gridx = 1;
       lblC.gridy = 0;
       lblC.weightx = 0.5;
       lblC.weighty = 1;
       lblC.insets = new Insets(5, 10, 5, 10);
       //field constraint
       GridBagConstraints fc = new GridBagConstraints();
       fc.anchor = GridBagConstraints.WEST;
       fc.gridx = 7;
       fc.gridy = 0;
       fc.weightx = 0.5;
       fc.weighty = 1;
       fc.insets = new Insets(5, 10, 5, 10);

       ResultPan.add(rnamelbl,lblC);
       ResultPan.add(unamelbl, fc);

       lblC.gridy++;
       ResultPan.add(rpassportlbl,lblC);

       fc.gridy++;
       ResultPan.add(upassportlbl,fc); 

       lblC.gridy++;
       ResultPan.add(rcontactlbl,lblC);

       fc.gridy++;
       ResultPan.add(ucontactlbl,fc); 

       lblC.gridy++;
       ResultPan.add(remaillbl,lblC);

       fc.gridy++;
       ResultPan.add(uemaillbl,fc);

       lblC.gridy++;
       ResultPan.add(rtouristnumlbl,lblC);

       fc.gridy++;
       ResultPan.add(utouristnumlbl,fc);

       lblC.gridy++;
       ResultPan.add(rseasonlbl,lblC);

       fc.gridy++;
       ResultPan.add(useasonlbl,fc);

       lblC.gridy++;
       ResultPan.add(rregionlbl,lblC);

       fc.gridy++;
       ResultPan.add(uregionlbl,fc);

       lblC.gridy++;
       ResultPan.add(rmeallbl,lblC);

       fc.gridy++;
       ResultPan.add(umeallbl,fc);

       lblC.gridy++;
       ResultPan.add(travelcostlbl,lblC);

       fc.gridy++;
       ResultPan.add(utravelcostlbl,fc);

       lblC.gridy++;
       ResultPan.add(tourguidelbl,lblC);

       fc.gridy++;
       ResultPan.add(utourguidelbl,fc);

    }
Shadow
  • 11
  • 3
  • 1
    Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – oleksa Feb 03 '20 at 09:07
  • Trying out now.. – Shadow Feb 03 '20 at 09:16
  • I sort of understand but my input fields is in a different java class and my output field is in another java class. How to link without causing a bug? – Shadow Feb 03 '20 at 09:31
  • you are posting a lot of code but I can not check it for null values. Please check the error stack from the message `Mainsystem.java:53` `Inputpanel.java:146`. Those numbers 53 and 146 shows you line in the source code where error was raised. You have to take a look on those lines and check what can raise `nullPointerException` – oleksa Feb 03 '20 at 09:36
  • I know the label is causing the problem. However, I am trying to get the data from input panel to be transfer to a label so that it can display its value. But I believe the code is written at the wrong place but I am unsure where to display other than the main place where can get text input directly from event occurred. – Shadow Feb 03 '20 at 09:42

0 Answers0