-2

I know the title might not be that helpful. I do apologize. My problem is that I am trying to create a simple MPG application to help me get used to GUI. I have a gallons and a miles JTextField. I also have an ueditable JTextField to display the MPG. I am trying to convert the user input to an integer then divide the gallons and miles input for the result of the miles per gallon, which will be displayed.

I have the parseInt as displayed below:

    int mil = Integer.parseInt(miles1.getText());
    int gal = Integer.parseInt(gallons1.getText());

then the MPG:

int mpg = gal / mil;

It shows no error in the code, but once I go to run the application I get these errors:

java.lang.NullPointerException
at app.<init>(app.java:40)
at app$1.run(app.java:26)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Does anybody know what is causing this?

alex2410
  • 10,904
  • 3
  • 25
  • 41
engz
  • 45
  • 1
  • 1
  • 10
  • 3
    `miles1` or `gallons1` is `null` (or both). – Alexis C. Feb 17 '14 at 19:26
  • You should probably show the code on or about line 40 of app.java, since that's where your NullPointerException occurs. – Samhain Feb 17 '14 at 19:28
  • @Samhain line 40 is ` int mil = Integer.parseInt(miles1.getText());` – engz Feb 17 '14 at 19:30
  • @user3320501 Then miles1 is null. You haven't initialized it yet. – Samhain Feb 17 '14 at 19:35
  • @Samhain how exactly do you initialize it? I would like it to take the user input of the two JTextfields then display them in an uneditable JTextfield once a button is clicked. – engz Feb 17 '14 at 19:37

2 Answers2

0

First of all u said that u have two textareas named gallons and miles but u used the identifiers gallons1 and miles1.

If thats not the case then, try initialising the gallons and miles jtextarea.

for e.g you may see something like these

    JTextArea gallons1;
    JTextArea miles1;

Try making them these respectively

    JTextArea gallons1=new JTextArea();
    JTextArea miles1=new JTextArea();
0

It sounds like the issue is that the JTextFields are null when you are calling .getText(). Make sure that they have a value when doing the math on the fields and you will be fine.

If you want to avoid errors completely, use if (miles1.matches("[0-9]*) { logic } and it won't throw an error for the Integer.parseInt if it's input incorrectly (The regex will fail since it's non-numeric).

Paul Muir
  • 304
  • 4
  • 9