Java Swing - NullPointerException when adding widgets with constraints -


this question has answer here:

i started learn swing, , encountered nullpointerexception when try use constraints in adding widgets content.

error trace:

nullpointerexception @ chapter.pkg6.improvedforcecalculator.<init>(improvedforcecalculator.java:56) @ chapter.pkg6.improvedforcecalculator.main(improvedforcecalculator.java:36) 

here code:

import java.awt.container; import java.awt.gridbaglayout; import java.awt.gridbagconstraints; import java.awt.font;  import java.awt.event.actionevent; import java.awt.event.actionlistener;  import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jtextfield; import javax.swing.jbutton; import javax.swing.joptionpane;  public class improvedforcecalculator {     private jframe frame;     private container content;     private gridbagconstraints constraints;      private jlabel masslabel;     private jlabel accelerationlabel;      private jtextfield massinput;     private jtextfield accelerationinput;      private jbutton calculateforce;      private double mass;     private double acceleration;     private double force;      public static void main(string[] args)     {         new improvedforcecalculator();     }      public improvedforcecalculator()     {         frame = new jframe();         frame.settitle("improved force calulcator");         frame.setresizable(false);         frame.setdefaultcloseoperation( jframe.exit_on_close );          constraints = new gridbagconstraints();          content = frame.getcontentpane();         content.setlayout(new gridbaglayout());          masslabel = new jlabel("enter mass of object (m): ");         masslabel.setfont(new font("times new roman", font.plain, 14));         content.add(masslabel);         constraints.gridx = 0;         constraints.gridy = 0;         content.add(massinput, constraints);          massinput = new jtextfield();         massinput.setfont(new font("times new roman", font.plain, 14));         constraints.gridx = 1;         constraints.gridy = 0;         content.add(massinput, constraints);          accelerationlabel = new jlabel("enter acceleration of object (a): ");         accelerationlabel.setfont(new font("times new roman", font.plain, 14));         constraints.fill = gridbagconstraints.horizontal;         constraints.gridx = 0;         constraints.gridy = 1;         content.add(accelerationlabel, constraints);          accelerationinput = new jtextfield();         accelerationinput.setfont(new font("times new roman", font.plain, 14));         constraints.gridx = 1;         constraints.gridy = 1;         content.add(accelerationinput, constraints);          calculateforce = new jbutton("calculate force");         calculateforce.setfont(new font("times new roman", font.plain, 14));         calculateforce.addactionlistener (new actionlistener(){             public void actionperformed(actionevent e)             {                 try                 {                     mass = double.parsedouble(massinput.gettext());                     acceleration = double.parsedouble(accelerationinput.gettext());                     force = mass * acceleration;                      system.out.println("force: " + force + " newtons");                 }                 catch (numberformatexception ex)                 {                     joptionpane.showmessagedialog(frame, "please enter valid mass and/or acceleration");                 }             }         });         constraints.gridx = 0;         constraints.gridy = 2;         constraints.gridwidth = 2;         content.add(calculateforce, constraints);          frame.pack();         frame.setvisible(true);             } } 

you have null pointer exception because try use massinput before initialize it. see here:

  content.add(massinput, constraints);      massinput = new jtextfield(); 

i think meant first line content.add(masslabel, constraints) instead. initialize massinput before adding content.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -