import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.math.*;
import java.text.*;
import java.io.*;
import java.util.*;
public class InvGUI extends JFrame
{
//Instantiate new arrays
static private int[] itemNumber = new int[7];
static private String[] itemName = new String[7];
static private int[] inStock = new int[7];
static private double[] manufPrice = new double[7];
static private double[] sellingPrice = new double[7];
static private Inventory[] inv = new Inventory[7];
//create ButtonHandlers
//private PrintButtonHandler pbHandler;
private ExitButtonHandler ebHandler;
//private SellButtonHandler sbHandler;
private SortButtonHandler sortbHandler;
//set GUI width and Height
private static final int WIDTH = 600;
private static final int HEIGHT = 400;
//Create JLabels
private JLabel itemsInStoreL;
//Create JTextFields
private JTextArea mainTextTA, reportTextTA;
//Create Jbuttons
private JButton sortB, sellB, printB, exitB;
//private ButtonHandler bHandler;
public InvGUI()
{
setTitle("Inventory"); //set the window title
setSize(WIDTH, HEIGHT); //set the window size
Container pane = getContentPane(); //get the container
pane.setLayout(null); //set container layout to null
// Create labels
itemsInStoreL= new JLabel( "The Items in the store",SwingConstants.LEFT);
//Create textfields
mainTextTA = new JTextArea(6, 20);
//Create Print Button
// printB = new JButton("Print Report");
// pbHandler = new PrintButtonHandler();
// printB.addActionListener(pbHandler);
//create Sell Button
// sellB = new JButton("Sell Item");
// sellbHandler = new SellButtonHandler();
// sellB.addActionListener(sbHandler);
//create Sort Button
sortB = new JButton("Sort By Name");
sortbHandler = new SortButtonHandler();
sortB.addActionListener(sortbHandler);
//Create Exit Button
exitB = new JButton("Exit");
ebHandler = new ExitButtonHandler();
exitB.addActionListener(ebHandler);
//set the size of the labels, text fields, and buttons
itemsInStoreL.setSize(200, 30);
mainTextTA.setSize(370, 120);
reportTextTA.setSize(370,120);
sortB.setSize(100, 30);
sellB.setSize(100, 30);
printB.setSize(100, 30);
exitB.setSize(100, 30);
//set the locations
itemsInStoreL.setLocation(220, 10);
mainTextTA.setLocation(20, 40);
sortB.setLocation(500, 40);
sellB.setLocation(500, 80);
printB.setLocation(500, 120);
exitB.setLocation(500, 160);
//add the labels, text fields, and buttons
pane.add(itemsInStoreL);
pane.add(mainTextTA);
pane.add(sortB);
pane.add(sellB);
pane.add(printB);
pane.add(exitB);
setVisible(true); //see the window
setDefaultCloseOperation(EXIT_ON_CLOSE); //exit on close
}
//create main method
public static void main(String[] args)
throws FileNotFoundException
{
InvGUI inventory = new InvGUI();
Scanner inFile = new Scanner ( new FileReader("final.txt"));
for (int i = 0; i < itemNumber.length; i++)
{
itemNumber = inFile.nextInt();
inFile.nextLine(); //dummy read
itemName = inFile.nextLine();
inStock = inFile.nextInt();
manufPrice = inFile.nextInt();
sellingPrice = inFile.nextInt();
}
for (int i = 0; i < inv.length; i++)
{
inv.setI(itemName, itemNumber, inStock, inStock, 0,
manufPrice, sellingPrice);
}
}
//Get file input using for loop
// for (int i = 0; i < itemNumber.length; i++)
// {
// itemNumber = inFile.nextInt();
// inFile.nextLine(); //dummy read
// itemName = inFile.nextLine();
// inStock = inFile.nextInt();
// values1 = inFile.nextInt();
// values2 = inFile.nextInt();
// }
//sort Button handler
private class SortButtonHandler implements ActionListener
{
public void actionPerformed( ActionEvent e)
{
String temp;
int counter, index;
for (counter = 0; counter < itemName.length - 1; counter++)
{
for (index = 0; index < itemName.length - 1 - counter;
index++)
if (itemName[index].compareTo(itemName[index + 1]) > 0 )
{
temp = itemName[index];
itemName[index] = itemName[index + 1];
itemName[index + 1] = temp;
}
}
}
}
// sell button handler
/* private class SellButtonHandler implements ActionListener
{
public void actionPerformerd ( ActionEvent e)
{
String str = console.next();
if (str.hasNext())
{
inStock - sell;
}
}
}*/
private class ExitButtonHandler implements ActionListener
{
public void actionPerformed( ActionEvent e)
{
System.exit(0);
}
}
}
🔒 Locked
Null Pointer Exception?
Advertisement
Original Post
Hey, I'm encountering a null pointer excepter. I'm new in JAVA language but I can't seem to find out what it is?...this program is "FAR" away from being done but I'm just trying to figure out why I'm getting the error: Exception in thread "main" java.lang.NullPointerException at InvGUI.<init>(InvGUI.java:84) at InvGUI.main(InvGUI.java:118)
This would be
The reportTextTA variable isn't initialized.
reportTextTA.setSize(370,120);The reportTextTA variable isn't initialized.
Well this is my updated code for right now. I dont know how to really set the text into the TestArea yet but i'm still working on it. I actually am really far from anything i really want. I want a text area that displays an items information, which will be read in from an inFile. I also want four buttons to sort by alphabetical order, sell button to sell an item, exit button and a print report button that prints the report into a different text area.
I am very very new to using GUI's but I just want to try getting this done so i can understand it better. I am also using inheritance ( base and derived classes)
The code(as of now) is:
I am very very new to using GUI's but I just want to try getting this done so i can understand it better. I am also using inheritance ( base and derived classes)
The code(as of now) is:
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.lang.Math.*;import java.math.*; import java.text.*;import java.io.*;import java.util.*;public class InvGUI extends JFrame { //Instantiate new arrays static private int[] itemNumber = new int[7]; static private String[] itemName = new String[7]; static private int[] inStock = new int[7]; static private double[] manufPrice = new double[7]; static private double[] sellingPrice = new double[7]; static private Inventory[] inv = new Inventory[7]; //create ButtonHandlers //private PrintButtonHandler pbHandler; private ExitButtonHandler ebHandler; //private SellButtonHandler sbHandler; private SortButtonHandler sortbHandler; //set GUI width and Height private static final int WIDTH = 600; private static final int HEIGHT = 400; //Create JLabels private JLabel itemsInStoreL; //Create JTextFields private JTextArea mainTextTA, reportTextTA; //Create Jbuttons private JButton sortB /*sellB*/, printB, exitB; //private ButtonHandler bHandler; public InvGUI() { setTitle("Inventory"); //set the window title setSize(WIDTH, HEIGHT); //set the window size Container pane = getContentPane(); //get the container pane.setLayout(null); //set container layout to null // Create labels itemsInStoreL= new JLabel( "The Items in the store",SwingConstants.LEFT); //Create textfields mainTextTA = new JTextArea(8, 20); reportTextTA = new JTextArea(8, 20); //Create Print Button //printB = new JButton("Print Report"); //pbHandler = new PrintButtonHandler(); //printB.addActionListener(pbHandler); //create Sell Button // sellB = new JButton("Sell Item"); // sellbHandler = new SellButtonHandler(); // sellB.addActionListener(sbHandler); //create Sort Button sortB = new JButton("Sort By Name"); sortbHandler = new SortButtonHandler(); sortB.addActionListener(sortbHandler); //Create Exit Button exitB = new JButton("Exit"); ebHandler = new ExitButtonHandler(); exitB.addActionListener(ebHandler); //set the size of the labels, text fields, and buttons itemsInStoreL.setSize(200, 30); mainTextTA.setSize(370, 120); reportTextTA.setSize(370,120); sortB.setSize(100, 30); //sellB.setSize(100, 30); printB.setSize(100, 30); exitB.setSize(100, 30); //set the locations itemsInStoreL.setLocation(220, 10); mainTextTA.setLocation(20, 40); sortB.setLocation(500, 40); //sellB.setLocation(500, 80); printB.setLocation(500, 120); exitB.setLocation(500, 160); //add the labels, text fields, and buttons pane.add(itemsInStoreL); pane.add(mainTextTA); pane.add(reportTextTA); pane.add(sortB); //pane.add(sellB); pane.add(printB); pane.add(exitB); setVisible(true); //see the window setDefaultCloseOperation(EXIT_ON_CLOSE); //exit on close } //create main method public static void main(String[] args) throws FileNotFoundException { InvGUI inventory = new InvGUI(); Scanner inFile = new Scanner ( new FileReader("final.txt")); for (int i = 0; i < itemNumber.length; i++) { itemNumber = inFile.nextInt(); inFile.nextLine(); //dummy read itemName = inFile.nextLine(); inStock = inFile.nextInt(); manufPrice = inFile.nextInt(); sellingPrice = inFile.nextInt(); } for (int i = 0; i < inv.length; i++) { inv.setI(itemName, itemNumber, inStock, inStock, 0, manufPrice, sellingPrice); } } //print button handler private class PrintButtonHandler implements ActionListener { reportTextTA.setText(printInv()); } //private class SellButtonHandler implements ActionListener //{ //} //sort Button handler private class SortButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e) { String temp; int counter, index; for (counter = 0; counter < itemName.length - 1; counter++) { for (index = 0; index < itemName.length - 1 - counter; index++) if (itemName[index].compareTo(itemName[index + 1]) > 0 ) { temp = itemName[index]; itemName[index] = itemName[index + 1]; itemName[index + 1] = temp; } } } } private class ExitButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e) { System.exit(0); } } }
A few stylistic notes.
[edit - this critique was done on your original code, I haven't looked at your updated code. If I get time I may extend this critique to any changes ]
Instead of maintaining several parralel arrays of item information, consider making a class that holds each of the values as members, then creating an array of these. However, in this case it would appear that this is unnecessary. Instead of filling these arrays from the file data, why not the following:
Or better still, a container. Unless you can justify the rather odd choice of "7" for the number of items available? [grin]
Consider using Collections.sort() or Arrays.sort() rather than writing your own sort routines. You can use a custom Comparator object to sort purely on item names, if you want your Inventory to have a different natural ordering (or no natural ordering).
You don't need to keep references to everything as members. For example, your action listeners and components could mostly be local variables. This would mean that your InvGUI would only need to store the reference to the inventory container as the sole member, which is cleaner.
Don't use "static" for this member, instead use a local inside main(). Then, pass it to the InvGui instance in the constructor. Let the InvGui save this reference.
This also removes a bug from your application. Your current code creates the GUI, and then reads the data. There doesn't appear to be any feedback to notify the GUI to update itself with the changes. I appreciate that the GUI doesn't appear to be finished yet, so maybe you haven't come across this.
[edit - this critique was done on your original code, I haven't looked at your updated code. If I get time I may extend this critique to any changes ]
Instead of maintaining several parralel arrays of item information, consider making a class that holds each of the values as members, then creating an array of these. However, in this case it would appear that this is unnecessary. Instead of filling these arrays from the file data, why not the following:
for (int i = 0; i < inv.length; i++){ int itemNumber = inFile.nextInt(); inFile.nextLine(); //dummy read String itemName = inFile.nextLine(); int inStock = inFile.nextInt(); int manufPrice = inFile.nextInt(); int sellingPrice = inFile.nextInt(); inv.setI(itemName, itemNumber, inStock, inStock, 0, manufPrice, sellingPrice); }Or better still, a container. Unless you can justify the rather odd choice of "7" for the number of items available? [grin]
Consider using Collections.sort() or Arrays.sort() rather than writing your own sort routines. You can use a custom Comparator object to sort purely on item names, if you want your Inventory to have a different natural ordering (or no natural ordering).
You don't need to keep references to everything as members. For example, your action listeners and components could mostly be local variables. This would mean that your InvGUI would only need to store the reference to the inventory container as the sole member, which is cleaner.
Don't use "static" for this member, instead use a local inside main(). Then, pass it to the InvGui instance in the constructor. Let the InvGui save this reference.
This also removes a bug from your application. Your current code creates the GUI, and then reads the data. There doesn't appear to be any feedback to notify the GUI to update itself with the changes. I appreciate that the GUI doesn't appear to be finished yet, so maybe you haven't come across this.
so these Null pointer exceptioins really get me....
i'm sorry i keep reposting my code and asking the same question but I dont understand why i keep getting a null pointer exception...
Exception in thread "main" java.lang.NullPointerException
at InvGUI.<init>(InvGUI.java:97)
at InvGUI.main(InvGUI.java:133)
import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.lang.Math.*;import java.math.*; import java.text.*;import java.io.*;import java.util.*;import java.util.Arrays.*;import java.util.Collections.*;import java.util.List.*;public class InvGUI extends JFrame { //Instantiate new arrays //static private int[] itemNumber = new int[7]; //static private String[] itemName = new String[7]; //static private int[] inStock = new int[7]; //static private double[] manufPrice = new double[7]; //static private double[] sellingPrice = new double[7]; //static private Inventory[] inv = new Inventory[7]; //create ButtonHandlers private PrintButtonHandler pbHandler; private ExitButtonHandler ebHandler; //private SellButtonHandler sbHandler; //private SortButtonHandler sortbHandler; //set GUI width and Height private static final int WIDTH = 600; private static final int HEIGHT = 400; //Create JLabels private JLabel itemsInStoreL; //Create JTextFields private JTextField sellTF; //Create JTextAreas private JTextArea mainTextTA, reportTextTA; //Create Jbuttons private JButton sortB, sellB, printB, exitB; //private ButtonHandler bHandler; public InvGUI() { setTitle("Inventory"); //set the window title setSize(WIDTH, HEIGHT); //set the window size Container pane = getContentPane(); //get the container pane.setLayout(null); //set container layout to null // Create labels itemsInStoreL= new JLabel( "The Items in the store",SwingConstants.LEFT); //create textfields sellTF = new JTextField(10); //Create textareas mainTextTA = new JTextArea(10, 10); reportTextTA = new JTextArea(10, 10); //Create Print Button printB = new JButton("Print Report"); pbHandler = new PrintButtonHandler(); printB.addActionListener(pbHandler); //create Sell Button // sellB = new JButton("Sell Item"); // sellbHandler = new SellButtonHandler(); // sellB.addActionListener(sbHandler); //create Sort Button //sortB = new JButton("Sort By Name"); //sortbHandler = new SortButtonHandler(); //sortB.addActionListener(sortbHandler); //Create Exit Button exitB = new JButton("Exit"); ebHandler = new ExitButtonHandler(); exitB.addActionListener(ebHandler); //set the size of the labels, text fields, and buttons itemsInStoreL.setSize(200, 30); mainTextTA.setSize(370, 120); reportTextTA.setSize(370,120); sellTF.setSize(100, 100); sortB.setSize(100, 100); //sellB.setSize(100, 30); printB.setSize(100, 30); exitB.setSize(100, 30); //set the locations itemsInStoreL.setLocation(220, 10); mainTextTA.setLocation(20, 40); sortB.setLocation(500, 40); //sellB.setLocation(20, 200); sellTF.setLocation(130, 200); printB.setLocation(500, 120); exitB.setLocation(500, 160); //add the labels, text fields, and buttons pane.add(itemsInStoreL); pane.add(mainTextTA); pane.add(reportTextTA); pane.add(sortB); //pane.add(sellB); pane.add(sellTF); pane.add(printB); pane.add(exitB); setVisible(true); //see the window setDefaultCloseOperation(EXIT_ON_CLOSE); //exit on close } //create main method public static void main(String[] args) throws FileNotFoundException { InvGUI inventory = new InvGUI(); Inventory[] inv = new Inventory[7]; Scanner inFile = new Scanner ( new FileReader("final.txt")); for (int i = 0; i < inv.length; i++) { int itemNumber = inFile.nextInt(); inFile.nextLine(); //dummy read String itemName = inFile.nextLine(); int inStock = inFile.nextInt(); int manufPrice = inFile.nextInt(); int sellingPrice = inFile.nextInt(); inv.setI(itemName, itemNumber, inStock, inStock, 0, manufPrice, sellingPrice); } } //print button handler private class PrintButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e) { if (e.getActionCommand().equals("Print Report")) reportTextTA.setText(toString()); } } /* private class SortButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e) { Arrays.sort(inv[]); } }*/ //private class SellButtonHandler implements ActionListener //{ //} //sort Button handler /* private class SortButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e) { String temp; int counter, index; for (counter = 0; counter < itemName.length - 1; counter++) { for (index = 0; index < itemName.length - 1 - counter; index++) if (itemName[index].compareTo(itemName[index + 1]) > 0 ) { temp = itemName[index]; itemName[index] = itemName[index + 1]; itemName[index + 1] = temp; } } } } */ private class ExitButtonHandler implements ActionListener { public void actionPerformed( ActionEvent e) { System.exit(0); } } }i'm sorry i keep reposting my code and asking the same question but I dont understand why i keep getting a null pointer exception...
Exception in thread "main" java.lang.NullPointerException
at InvGUI.<init>(InvGUI.java:97)
at InvGUI.main(InvGUI.java:133)
When you make an array of objects in Java you still have to individually initialize the members of the array. Your array of strings should be ok because each element is assigned to a the string returned by nextLine. I don't see the inv array elements being initialized before they are used. This tutorial seems to explain it well:
http://www.javafaq.nu/java-article651.html
http://www.javafaq.nu/java-article651.html
When you see an error and it gives you:
Exception in thread "main" java.lang.NullPointerException
at InvGUI.<init>(InvGUI.java:84)
at InvGUI.main(InvGUI.java:118)
The last two lines are what is called a stack trace. The very first line of which is exactly where the problem happened. The second line is the function that called the function listed in the first. If that doesn't point out the problem enough for you to fix it have you could always try using a debugger.
Exception in thread "main" java.lang.NullPointerException
at InvGUI.<init>(InvGUI.java:84)
at InvGUI.main(InvGUI.java:118)
The last two lines are what is called a stack trace. The very first line of which is exactly where the problem happened. The second line is the function that called the function listed in the first. If that doesn't point out the problem enough for you to fix it have you could always try using a debugger.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement