Forum Stats

  • 3,770,456 Users
  • 2,253,116 Discussions
  • 7,875,463 Comments

Discussions

Adding a UserDefined Object in ComboBox

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Sep 27, 2016 3:14PM in New To Java

Hi,

I have created a user defined class car and i am trying to add its objects in a combo box on a button click but i a m getting "Null Pointer Exception" when i am running this program. Following is my code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class Car{

String carModel;

double price;

Car(String carModel1, double p){

carModel=carModel1;

price = p;

}

}

public class UseCar extends JFrame{

   JComboBox<Car> cbox;

   DefaultComboBoxModel<Car> model;

   JFrame frame = new JFrame("Test");

   private JButton button;

   UseCar() {

  

      frame.setSize(500,200);

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      JPanel panel = new JPanel();

      frame.add(panel);

      button=new JButton("Hello");

      panel.add(button);

      cbox= new JComboBox<>();

      panel.add( cbox);

      pack();

      MyActionListener listener=new MyActionListener(cbox, model);   //added

      button.addActionListener(listener);

      frame.setVisible(true);

   }

   public static void main(String args[ ]) {

       // Run the GUI construction in the Event-Dispatching thread for thread-safety

      SwingUtilities.invokeLater(new Runnable() {

         @Override

         public void run() {

            new UseCar(); // Let the constructor do the job

         }

      });

}

}

class MyActionListener implements ActionListener{

MyActionListener(JComboBox<Car> cbox, DefaultComboBoxModel<Car> model){

   Car car1 = new Car("2010", 300.0);

   Car car2 = new Car("2011", 400.0);

   Car car3 = new Car("2012", 500.0);

   Car car4 = new Car("2013", 600.0);

   Car car5 = new Car("2014", 700.0);

  

   model.addElement(car1);

   /*model.addElement(car2);

   model.addElement(car3);

   model.addElement(car4);

   model.addElement(car5);

   cbox.setModel(model);*/

}

        public void actionPerformed (ActionEvent e) {

        System.out.println("pressed button");

        }

}

Following is my error message:

java UseCar

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

        at MyActionListener.<init>(UseCar.java:54)

        at UseCar.<init>(UseCar.java:31)

        at UseCar$1.run(UseCar.java:40)

        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)

        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)

        at java.awt.EventQueue.access$500(EventQueue.java:97)

        at java.awt.EventQueue$3.run(EventQueue.java:709)

        at java.awt.EventQueue$3.run(EventQueue.java:703)

        at java.security.AccessController.doPrivileged(Native Method)

        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)

        at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)

        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)

        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)

        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)

        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)

        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

Somebody please guide me.

Zulfi.

Answers

  • TPD-Opitz
    TPD-Opitz Member Posts: 2,465 Silver Trophy
    edited Sep 25, 2016 2:25PM

    could you please mark line 54 in your code?

    bye

    TPD

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Sep 25, 2016 11:47PM

    Thanks for your attention. I am able to find my error. Actually i was not creating object of DefaultComboBoxModel.

    I have added the following line & now its working.

    DefaultComboBoxModel<Car> model = new DefaultComboBoxModel<Car>();

    I am still working on because i want to see how can i use toString() method???

    Zulfi.

  • Unknown
    edited Sep 26, 2016 10:35AM
    I am able to find my error.

    Well - that's good.

    But you posted in the forums and ask for help.

    And when someone tries to help and asks questions you are expected to respond by answering their questions and providing more info.

    So which line is line #54?

    I am still working on because i want to see how can i use toString() method???

    I suggest you start learning Java by reading The Java Tutorials and working with the example code they provide.

    You can find specific topics with a simple web search like: 'The Java Tutorials string'

    https://docs.oracle.com/javase/tutorial/java/data/strings.html

    Strings

    Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.The Java platform provides the String  class to create and manipulate strings.

    Creating Strings

    The most direct way to create a string is to write:. . .

    There are trails that cover ALL of the basic Java functionality.

  • User_AYF65
    User_AYF65 Member Posts: 135 Red Ribbon
    edited Sep 26, 2016 2:09PM

    Hi my friend,

    Thanks for your help. Actually i found the line of error. You can see my code (a portion of which i am pasting again). However i feel sorry for not giving you proper answer. This is the line# 54

    model.addElement(car1);

    & my code:

    model.addElement(car1);

       /*model.addElement(car2);

       model.addElement(car3);

       model.addElement(car4);

       model.addElement(car5);

       cbox.setModel(model);*/

    You can see i commented all the lines above it. Anyway i kept thinking about it and during the morning i realized my mistake. So i just checked it whether it would work or not. However it worked. Then i posted the solution. Sorry for ignoring your Q.

    Thanks again. I am not able to solve this yet. I would try again in a day or two. Then i would show you solution or  any problem related to it.

    Zulfi.

  • Unknown
    edited Sep 26, 2016 3:04PM

    Thanks for the update.

    Thanks again. I am not able to solve this yet. I would try again in a day or two. Then i would show you solution or any problem related to it.

    That would be good if you get the chance. It often helps others that have the same, or similar, problem to know how the error was found and what the fix was.

  • dvohra21
    dvohra21 Member Posts: 14,258 Gold Crown
    edited Sep 27, 2016 3:14PM

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

            at MyActionListener.<init>

    indicates some variable is not initialized.

This discussion has been closed.