Java GUI creating objects with arguments taken from text fields

1.6k views Asked by At

so I have this Jframe where you fill out the text fields and when click "Add" it should create a Car object and send it to the database. But in the listener i keepk getting java.lang.NullPointerException on the textfieldname.getText() even though i input some kind of text in the field.

Here is the Jframe.

public void newCar()
   {

      JLabel make = new JLabel("Brand:");
      JTextField maket = new JTextField(10);

      JLabel model = new JLabel("Model:");
      JTextField modelt = new JTextField(10);

      JLabel license = new JLabel("License Plate Numbers:");
      JTextField licenset = new JTextField(10);

      JLabel color = new JLabel("Color:");
      JTextField colort = new JTextField(10);

      JLabel year = new JLabel("Year:");
      final JTextField yeart = new JTextField(10);
      yeart.addKeyListener(new KeyAdapter()
      {
         @Override
         public void keyTyped(KeyEvent e)
         {
            super.keyTyped(e);

            e.getKeyCode();

            if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
            {
               e.consume();
            }
         }

      });
      JLabel horse = new JLabel("Horse Power: ");
      JTextField horset = new JTextField(10);
      JLabel isAvailable = new JLabel("Car Status:");
      JLabel isAvailablet = new JLabel("Available");
      JLabel time = new JLabel("Time Until Service: ");
      JTextField timet = new JTextField(10);
      JLabel consumption = new JLabel("Consumption per 100km: ");
      JTextField consumptiont = new JTextField(10);
      JLabel seats = new JLabel("Number of Seats: ");
      JTextField seatst = new JTextField(10);
      seatst.addKeyListener(new KeyAdapter()
      {
         @Override
         public void keyTyped(KeyEvent e)
         {
            super.keyTyped(e);

            e.getKeyCode();

            if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
            {
               e.consume();
            }
         }

      });

      JLabel transmission = new JLabel("Transmission ");
      JComboBox transmissiont = new JComboBox<String>();
      JLabel climate = new JLabel("Climate Control: ");
      JComboBox climatet = new JComboBox<String>();

      JFrame window = new JFrame("New Car");

      JPanel newCarButtons = new JPanel();

      newCarButtons.setLayout(new FlowLayout());

      saveCar = new JButton("Save");
      saveCar.addActionListener(buttonListener);
      newCarButtons.add(saveCar);

      cancelCar = new JButton("Cancel");
      cancelCar.addActionListener(buttonListener);
      newCarButtons.add(cancelCar);

      JPanel newCarText = new JPanel();

      GroupLayout group = new GroupLayout(newCarText);
      newCarText.setLayout(group);

      group.setAutoCreateGaps(true);
      group.setAutoCreateContainerGaps(true);

      newCarText.add(make);
      newCarText.add(maket);

      newCarText.add(model);
      newCarText.add(modelt);

      newCarText.add(license);
      newCarText.add(licenset);

      newCarText.add(color);
      newCarText.add(colort);

      newCarText.add(year);
      newCarText.add(yeart);

      newCarText.add(horse);
      newCarText.add(horset);

      newCarText.add(isAvailable);
      newCarText.add(isAvailablet);

      newCarText.add(time);
      newCarText.add(timet);

      newCarText.add(consumption);
      newCarText.add(consumptiont);

      newCarText.add(seats);
      newCarText.add(seatst);

      JLabel doors = new JLabel("Number of Doors: ");
      JTextField doorst = new JTextField(10);

      doorst.addKeyListener(new KeyAdapter()
      {
         @Override
         public void keyTyped(KeyEvent e)
         {
            super.keyTyped(e);

            e.getKeyCode();

            if (!((int) e.getKeyChar() >= 48 && (int) e.getKeyChar() <= 57))
            {
               e.consume();
            }
         }

      });

      newCarText.add(doors);
      newCarText.add(doorst);

      transmissiont.addItem("Auto");
      transmissiont.addItem("Manual");

      transmissiont.addActionListener(buttonListener);

      newCarText.add(transmission);
      newCarText.add(transmissiont);

      climatet.addItem("Yes");
      climatet.addItem("No");

      newCarText.add(climate);
      newCarText.add(climatet);

      GroupLayout.SequentialGroup hGroup = group.createSequentialGroup();

      hGroup.addGroup(group.createParallelGroup().addComponent(make)
            .addComponent(model).addComponent(license).addComponent(color)
            .addComponent(time).addComponent(consumption).addComponent(year)
            .addComponent(horse).addComponent(isAvailable).addComponent(seats)
            .addComponent(doors).addComponent(transmission)
            .addComponent(climate));

      hGroup.addGroup(group.createParallelGroup().addComponent(maket)
            .addComponent(modelt).addComponent(licenset).addComponent(colort)
            .addComponent(timet).addComponent(consumptiont).addComponent(yeart)
            .addComponent(isAvailablet).addComponent(horset)
            .addComponent(seatst).addComponent(doorst)
            .addComponent(transmissiont).addComponent(climatet));

      group.setHorizontalGroup(hGroup);

      GroupLayout.SequentialGroup vGroup = group.createSequentialGroup();

      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(make).addComponent(maket));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(model).addComponent(modelt));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(license).addComponent(licenset));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(color).addComponent(colort));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(year).addComponent(yeart));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(horse).addComponent(horset));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(time).addComponent(timet));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(consumption).addComponent(consumptiont));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(isAvailable).addComponent(isAvailablet));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(seats).addComponent(seatst));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(doors).addComponent(doorst));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(transmission).addComponent(transmissiont));
      vGroup.addGroup(group.createParallelGroup(Alignment.BASELINE)
            .addComponent(climate).addComponent(climatet));

      group.setVerticalGroup(vGroup);

      JPanel newCar = new JPanel();
      newCar.setLayout(new BorderLayout());

      newCar.add(newCarText, BorderLayout.NORTH);
      newCar.add(newCarButtons, BorderLayout.SOUTH);

      newCar.setBorder(new TitledBorder(BorderFactory
            .createLineBorder(Color.black), "[New Car]", 2, 0));


      window.add(newCar);
      window.setLocationRelativeTo(null);
      window.setSize(400, 450);
      window.setVisible(true);

   }

Button listener:(Ignore the inputed value, i was just testing if the object is sent at all so i had to input some values since i am having problems with the .getText())

 private class MyButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         if (e.getSource() == addCars)
         {
            newCar();

         }

         if (e.getSource() == saveCar)
         {
            adapter = new DatabaseAdapter2();

            // Car car1 = new Car("ss", "ss",
            // "sd", "ss",
            // 15, "ss","ss",
            // "ss", "ss", 2, 2, "ss",
            // "ss", 2);

            Car car1 = new Car(maket.getText(), "test", "test", "test", 2011,
                  "test", "test", "test", "test", 2011, 2011, "test", "test", 2);
            System.out.println(car1);

            adapter.addCar(car1);
         }
         if (e.getSource() == cancelCar)
         {

         }

         if (e.getSource() == getCars)
         {

         }
         if (e.getSource() == removeCars)
         {

         }
         if (e.getSource() == editCars)
         {

         }
         if (e.getSource() == infoCars)
         {

         }
         if (e.getSource() == addClients)
         {
            newClient();
         }
         if (e.getSource() == removeClients)
         {

         }
         if (e.getSource() == editClients)
         {

         }
         if (e.getSource() == infoClients)
         {

         }
         if (e.getSource() == addAutoCampers)
         {

         }
         if (e.getSource() == removeAutoCampers)
         {

         }
         if (e.getSource() == editAutoCampers)
         {

         }
         if (e.getSource() == infoAutoCampers)
         {

         }
         if (e.getSource() == addBookings)
         {
            newBooking();
         }
         if (e.getSource() == removeBookings)
         {

         }
         if (e.getSource() == editBookings)
         {

         }
         if (e.getSource() == infoBookings)
         {

         }

         // if (e.getSource() == transmissiont)
         // {
         // if (transmissiont.getSelectedItem())
         // {
         // Student temp = (Student)studentBox.getSelectedItem();
         // firstNameField.setText(temp.getFirstName());
         // lastNameField.setText(temp.getLastName());
         // }
         // }

      }
   }
}
1

There are 1 answers

0
MadProgrammer On

It looks like you have a context issue.

You've declared all your fields within the newCar method, meaning that those fields you're using on the screen aren't the same you as those you seem to be accessing within the actionPerformed method.

Try removing the JTextField before the variable names (in the newCar method), and where required, declare the missing fields as instance variables

You should also avoid using KeyListeners on text fields, by the time you're notified of the key event, it's already being applied to the fields Document.

Instead, you should consider using a DocumentFilter

Check out