setText() NULL pointer excception problem
I'm currently trying to implement a GUI ontop of my program which extracts information from a '.dat' file. However, when trying to extract the data from the classes responsible for this extraction a NULL pointer exception is thrown. I know where it is and why it is happening, however I am not sure how to solve it.
Code :
public class GUInterface extends JFrame implements ActionListener
{
//creating GUI variables
JLabel type, loc, size;
public void create()
{
guInterface();
}//end create
public void guInterface()
{
//initialising variables
type = new JLabel("Type");
loc = new JLabel("Loc");
size = new JLabel("Size");
add(type);
add(loc);
add(size);
}
public void indexOutput(String output)
{
size.setText(output);
}
}//end GUInterface
This is just a small section of the code used to create the GUI (for example, other JPanels and JTextAreas are used), however, I've tried to include all the main components that I believe relate to the problem.
The method indexOutput is called by another class which returns the file size of the file being read.
Code :
GUInterface gui = new GUInterface();
public void readFile(String fileLocation) throws Exception
{
output = "File Size: " + fileSize + " Bytes";
gui.indexOutput(output);
}
However, when ran, the size JLabel variable within the indexOutput() method is seen as null, therefore throwing the exception.
If anyone could offer any adive on this issue it would be appreciated.
Thanks,
David.
Re: setText() NULL pointer excception problem
The size variable (and loc and type) never get instantiated. You should do so in a constructor, or at least call create or guInterface() on an instance before accessing the variables in readFile