1

I'm in need of help for my programming project in Eclipse. Here is the GUI Screen:

https://i.stack.imgur.com/DD3Nc.jpg

When the "Done" Button in the GUI Interface is pressed, the user input in the editor pane would be displayed beside the "Result:" text. In this case, the word "test" would appear beside "Result", thereby becoming "Result: test".

Instead, I have an error called "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException", and the errors are highlighted in the pictures. I think the error is because I have a null value assigned to the variable "result", but I'm not sure how do I go about solving it.

Here is the StackTrace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controller.Controller.Done(Controller.java:16)
at Admin.Screen04$2.actionPerformed(Screen04.java:55)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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)

DataStorage:

package data;

import java.io.File;
import java.io.FileWriter;
import java.util.Vector;
import com.thoughtworks.xstream.XStream;

public class DataStorage {

private Vector<Info> Information;
private Vector<Person> personList;
private XStream xstream;

public DataStorage() {
    {
        this.Information = new Vector<Info>();
    }

    {
        this.personList = new Vector<Person>();
        this.xstream = new XStream();

        Person[] personArr = (Person[]) this.xstream.fromXML(new File("saves/persons.xml"));
        for (int i = 0; i < personArr.length; i++) {
            this.personList.add(personArr[i]);
        }
    }
}

public void saveData() {
    Person[] personArr = this.getAllPersons();
    String xml = this.xstream.toXML(personArr);

    try {
        FileWriter file = new FileWriter("saves/persons.xml");
        file.write(xml);
        ;
        file.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void addPerson(Person person) {
    this.addPerson(person);
}

private Person[] getAllPersons() {
    Person[] PerArr = new Person[this.personList.size()];
    this.personList.toArray(PerArr);
    return PerArr;
}

public void addData(Info records) {
    this.Information.add(records);
}

public Info[] getAllContents() {
    Info[] opArr = new Info[this.Information.size()];
    this.Information.toArray(opArr);
    return opArr;
}

public void deleteCalculation(int index) {
    this.Information.removeElementAt(index);
}

public void editCalculation(int index, Info newInfo) {
    Information.setElementAt(newInfo, index);

}
}

Info:

package data;

public class Info {

private String number1;
private String result;

public Info() {
    this.number1 = null;
    this.result = null;
}

public Info(String number1, String result) {
    this.number1 = number1;
    this.result = result;
}

public String getNumber1() {
    return number1;
}

public String getResult() {
    return result;
}

public static void main(String[] args) {

}

}

Controller:

package controller;

import data.DataStorage;
import data.Info;

public class Controller {

private DataStorage dataStorage;

public Controller() {
}

public String Done(String number1) {
    String result = number1;
    Info Records = new Info(number1, result);
    this.dataStorage.addData(Records);
    return result; 
}

public Info[] getAllContents() {
    return this.dataStorage.getAllContents();
}

public void deleteCalculation(int index) {
    this.dataStorage.deleteCalculation(index);

}

public void editCalculation(int index, data.Info Newcalc) {
    this.dataStorage.editCalculation(index, Newcalc);

}
}

Screen04 (Which is the GUI Screen):

package Admin;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;

import gui.MainFrame;

public class Screen04 extends JPanel {
private MainFrame mainFrame;
private JLabel lblResult_1;
private JEditorPane editorPane;

public Screen04(MainFrame main) {
    setBackground(Color.ORANGE);
    this.mainFrame = main;
    setLayout(null);

    JLabel lblAddTracker = new JLabel("Add Trackers");
    lblAddTracker.setForeground(Color.DARK_GRAY);
    lblAddTracker.setFont(new Font("Tahoma", Font.BOLD, 20));
    lblAddTracker.setBounds(107, 35, 142, 23);
    this.add(lblAddTracker);

    JButton btnBackToAdmin = new JButton("Back to Admin Page");
    btnBackToAdmin.setBackground(Color.DARK_GRAY);
    btnBackToAdmin.setForeground(Color.ORANGE);
    btnBackToAdmin.setFont(new Font("Tahoma", Font.BOLD, 15));
    btnBackToAdmin.setBounds(84, 355, 197, 23);
    btnBackToAdmin.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            showAccount2();
        }
    });
    this.add(btnBackToAdmin);

    editorPane = new JEditorPane();
    editorPane.setForeground(Color.ORANGE);
    editorPane.setBackground(Color.DARK_GRAY);
    editorPane.setBounds(25, 76, 303, 234);
    this.add(editorPane);

    JButton btnDone = new JButton("Done");
    btnDone.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String number1 = String.valueOf(editorPane.getText());
            String result = mainFrame.getController().Done(number1);
            btnDone.setForeground(Color.ORANGE);
            btnDone.setBackground(Color.DARK_GRAY);
            lblResult_1.setText("" + result);
        }
    });
    btnDone.setBounds(239, 312, 89, 25);
    add(btnDone);

    JLabel lblResult_1 = new JLabel("Result:");
    lblResult_1.setBounds(79, 321, 46, 14);
    add(lblResult_1);
}

protected void showAccount2() {
    this.mainFrame.showAccount2();
}
}

Any help would be greatly appreciated. Thank you in advance!

As an additional help, what should I do so that when the "Done" Button in the GUI Interface is pressed, the user input would be stored into a vector called "Info", as well as displaying the words "Successfully Added" beside the text "Result:"?

camickr
  • 321,443
  • 19
  • 166
  • 288
E. Wong
  • 11
  • 1
  • 1
    Take a look at [What is a NullPointerException, and how do I fix it](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). Also, please don't post all of your code here, post a [MCVE] that demonstrates the problem. – Lukas Rotter Aug 13 '16 at 13:29
  • 1
    See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Aug 13 '16 at 13:29
  • 2
    *"As an additional help,.."* Ask a new question! This is a Q&A site, not a help desk. – Andrew Thompson Aug 13 '16 at 13:30
  • 1
    The dataStorage variable is null. You should look up the link that @LukasRotter and learn to debug NPE. In fact it's a good practice to look up your exception and learn how to debug it before asking as most of them (like this one) are quite common. – Hovercraft Full Of Eels Aug 13 '16 at 13:38
  • Ah, I see... Thanks, everyone. Will take a look at the links and remember to format my code better in future. Kind of new to Stack and handling errors, so... xD Ah, I see, Andrew. Will take note. Thank you for the info. :) – E. Wong Aug 13 '16 at 14:14

0 Answers0