7 Replies - 1278 Views - Last Post: 01 May 2009 - 09:12 PM Rate Topic: -----

#1 Grayton1972  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-April 09

Issues with Exception in thread "main" java.lang.NullPointer

Posted 30 April 2009 - 11:47 PM

I am trying to set up a GUI for an inventory program. I have gotten the code to where it complies with no red marks for errors. When run the program I get the error message at the end of this post. Can someone please help?


import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class GUI extends JFrame
{

  private int currentProdno = 0;
  private int arraySize = 0;
  private double totValue;

  private JTextField jguiDvdItem; //Item nbr
  private JTextField jguiDvdName; //dvd name
  private JTextField jguDvdCount; //# dvd
  private JTextField jguDvdPrice; // unit price
  private JTextField jguiYear; //DVD year
  private JTextField jguiDvdRestock; //Restocking fee
  private JTextField jguiTotal;
  private NewDVD[ ] inventory;
  private JButton btnNext;  

  GUI( NewDVD[]inv )
  {

	arraySize = inv.length; 
	inventory = inv;

		JPanel jp; //Change the name of these variables to something else
		JLabel jl;
		JPanel outerPanel = new JPanel();
		outerPanel.setLayout(new BoxLayout(outerPanel, BoxLayout.Y_AXIS));

		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		jl = new JLabel("GUI for DVD Inventory"); 
		titlePanel.add(jl);


		JPanel numberPanel = new JPanel();
		numberPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		jl = new JLabel("  Item Number: " );//Label for the item number
		numberPanel.add(jl);
		jguiDvdItem = new JTextField (6);  //Size of field
		jguiDvdItem.setEditable(false); //Makes it "write protected"
		numberPanel.add(jguiDvdItem);
		outerPanel.add(numberPanel);

		JPanel number1Panel = new JPanel();
		number1Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		jl = new JLabel("   Name: " );//Name of item
		number1Panel.add(jl);
		jguiDvdName = new JTextField (16);
		jguiDvdName.setEditable(false);
		number1Panel.add(jguiDvdName);
		outerPanel.add(number1Panel);

		JPanel number2Panel = new JPanel();
		number2Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		jl = new JLabel(" # in stock: " );// Number of each item
		number2Panel.add(jl);
		jguDvdCount = new JTextField(4);
		jguDvdCount.setEditable(false);
		number2Panel.add(jguDvdCount);
		outerPanel.add(number2Panel);

		JPanel number3Panel = new JPanel();
		number3Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		jl = new JLabel("   Price: $ " );//price of each item
		number3Panel.add(jl);
		jguDvdPrice = new JTextField(4);
		jguDvdPrice.setEditable(false);
		number3Panel.add(jguDvdPrice);
		outerPanel.add(number3Panel);

		JPanel number4Panel = new JPanel();
		number4Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		jl = new JLabel("   Year: $ " );//Year
		number4Panel.add(jl);
		jguiYear = new JTextField(5);
		jguiYear.setEditable(false);
		number4Panel.add(jguiYear);
		outerPanel.add(number4Panel);

		JPanel number5Panel = new JPanel();
		number5Panel.setLayout(new FlowLayout(FlowLayout.LEFT));
		jl = new JLabel("Total Inventory Value: $" );//Total Inventory Value
		number5Panel.add(jl);
		jguiTotal = new JTextField(7);
		jguiTotal.setEditable(false);
		number5Panel.add(jguiTotal);
		outerPanel.add(number5Panel);

//******************************************
//Button Area:
		JPanel jpButtonN = new JPanel();
		JButton jbNext = new JButton("Next");
		jbNext.addActionListener(new NextButtonHandler());
		jpButtonN.add(jbNext);
		outerPanel.add(jpButtonN);


//*************************************************************************
	//Call update Fields
	updateNButton( inventory, currentProdno );
		updateFields( inventory, currentProdno );

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(300, 75, 550, 650);

		setContentPane(outerPanel);
		setResizable(false);
		setVisible(true);



	  }//end GUI

//*************************************************************************
  public void updateNButton( NewDVD[ ] inventory, int index )
	{
		totValue += inventory[ index ].getdvdRestock();

	}

//*************************************************************************
  public void updateNextButton( NewDVD[ ] inventory )
	{
	  int i = 0;
		
		for(i = 0;i <= (inventory.length-1);i++)
		{
			totValue += inventory[ i ].value();
		}

	}

//*************************************************************************


	//updateFields NEED 2 PARAMENTER THE OBJECT AND THE INDEX
	//TO SPECIFY THE ARRAY ELEMENT

  public void updateFields( NewDVD[ ] inventory, int index )
	{


		jguiDvdItem.setText( "" + inventory[ index ].getDvdItem() );//DVD Item umber
		jguiDvdName.setText( "" + inventory[ index ].getDvdName() );//DVD name
		jguDvdCount.setText( "" + inventory[ index ].getDvdCount() );//# DVDs
		jguDvdPrice.setText( "" + inventory[ index ].getDvdPrice() );// DVDPrice
		jguiDvdRestock.setText( "" + String.format("%.2f",inventory[ index ].getdvdRestock()) );//Restocking Fee
		jguiYear.setText( "" + inventory[ index ].getYear() );//Year
		jguiTotal.setText( "" + String.format("%.2f",totValue ));//Total Value

	} // end updateFields()

//**********************************************************
//Next Button
  class NextButtonHandler implements ActionListener
  {

	public void actionPerformed(ActionEvent event)	{
		if (currentProdno > inventory.length)
		{
			currentProdno = 0;
			totValue = 0;
			updateNButton( inventory, currentProdno );

		}
		else
		{
			++currentProdno;
			updateNButton( inventory, currentProdno );

		}

		updateFields( inventory, currentProdno);

	}
  }//end Next Button
}//end GUI



ERROR MESSAGE: Exception in thread "main" java.lang.NullPointerException
at GUI.updateFields(InventoryGUI.java:157)
at GUI.<init>(InventoryGUI.java:111)
at InventoryPart1.main(InventoryPart1.java:37)
Java Result: 1

Is This A Good Question/Topic? 0
  • +

Replies To: Issues with Exception in thread "main" java.lang.NullPointer

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Issues with Exception in thread "main" java.lang.NullPointer

Posted 01 May 2009 - 12:05 AM

i dont see a main method here
Was This Post Helpful? 0
  • +
  • -

#3 Grayton1972  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-April 09

Re: Issues with Exception in thread "main" java.lang.NullPointer

Posted 01 May 2009 - 12:12 AM

View Postmostyfriedman, on 30 Apr, 2009 - 11:05 PM, said:

i dont see a main method here


oops i only posted the GUI method


import java.text.*;


public class InventoryPart1
{//start InventoryPart1

	DecimalFormat formatter = new DecimalFormat("$##,###.00");
	static NewDVD[] arrayDVD = new NewDVD[3]; //3 unit array

	public static void main(String args [])
	{//start main method

		NewDVD dvd = null;
		double totalDVDprice = 0; //initiallize dvd price to 0

						//item, name, # in stock, price, year
		dvd = new NewDVD (7634, "Harry Potter",22, 20.99, 2006);//year not accurate
		arrayDVD[0] = dvd;
		dvd = new NewDVD (987, "Armageddon",18, 12.95, 2000);//year not accurate
		arrayDVD[1] = dvd;
		dvd = new NewDVD (872643, "Jurassic Park", 15, 13.89, 1990);//year not accurate
		arrayDVD[2] = dvd;

		//add new DVD information here

		sortLoop(arrayDVD);// initializes sorting method

		// create the GUI
		  GUI gui = new GUI( arrayDVD );

		System.out.print("\nINVENTORY LIST (DVD)\n\n");

			for ( int i = 0; i<arrayDVD.length; i++)
			{//Loop thru the arrayDVD
				 System.out.println("\nItem number: " + arrayDVD[i].getDvdItem());
				 System.out.println("Item Name: " + arrayDVD[i].getDvdName());
				 System.out.println("Year: " + arrayDVD[i].getYear());
				 System.out.println("Units in stock: " + arrayDVD[i].getDvdCount());
				 System.out.printf("Price per Unit: $%5.2f %n" , arrayDVD[i].getDvdPrice());
				 totalDVDprice = totalDVDprice + arrayDVD[i].value();
			}//end loop through array DVD

		System.out.printf("Total Inventory Value for DVDs: $%5.2f %n" ,  totalDVDprice );


		System.out.print("\n\nINVENTORY LIST(Total)\n\n");
		double TIV = CalcTotalValue();
		System.out.printf("Total Inventory Value: $%5.2f %n" , TIV );

	} //end main method
	public static double CalcTotalValue ()
	{//start total inventory value method
		double total = 0;

		for (int i=0; i<arrayDVD.length; i++)
			{// Add the DVD values
			total = total + arrayDVD[i].value();
			}

	return total;
	}//end total inventory value method

	public static void sortLoop(DVD[] x)
	{//stsrt sorting method
	  boolean doMore = true;
	  DVD temp = null;
	  int compareResult = 0;
	  while (doMore)
	  {//will run through sorting loop until names are in alphabetical order
		doMore = false;
		for (int i=0; i<x.length-1; i++)
		{
			compareResult = x[i].getDvdName().compareToIgnoreCase(x[i+1].getDvdName());
			if (compareResult > 0 )
			{//switches names until in order
			   temp = x[i];
			   x[i] = x[i+1];
			   x[i+1] = temp;
			   doMore = true;  // after switching names, checks again

			}
		}
	  }
	 }


} // end class InventoryPart1





public class DVD 

{// start class DVD

	public String dvdName;
	public int dvdCount;
	public double dvdPrice;
	public int dvdItem;


	public DVD(int item, String title, int count, double price)
	{
		dvdName = title;
		dvdCount = count;
		dvdPrice = price;
		dvdItem  = item;
	} //end constructor

	
	public void setDvdName(String title)
	{// set DVD name
		dvdName = title;
	} //end setDvdName

	public String getDvdName()
	{//start get dvd name
		return dvdName;
	} //end getDvdName

	public void setDvdCount(int count)
	{//start set dvd count
		dvdCount = count;
	} //end setDvdCount

	public int getDvdCount() 
	{//start get dvd count
		return dvdCount;
	} //end getDvdCount

	public void setDvdPrice(double price)
	{// start set dvdprice
		dvdPrice = price;
	} //end setDvdPrice

	public double getDvdPrice()
	{//start get dvdprice
		return dvdPrice;
	} //end getDvdPrice

	public void setDvdItem(int item)
	{//start setdvditem
		dvdItem = item;
	} //end setdvdItem

	public int getDvdItem()
	{//start get dvditem
		return dvdItem;
	} //end getDvdItem


	public double value()//calculate inventory
	 {
		return (dvdPrice * dvdCount);
	 } // end calculate invemtory

}//End class DVD





public class NewDVD extends DVD
{//starts subclass
	private int dvdYear;//unique item for subclass

	public NewDVD(int item, String title, int count, double price, int Year)
	{//initiates items from super class to be inherited into subclass
		super( item, title, count, price);
		dvdYear = Year;
	}
	@Override
	public double getDvdPrice()
	{//adds 5% restocking fee to total price
		return super.getDvdPrice() * 1.05;
	}

	public double getdvdRestock()
	{
		return super.getDvdPrice() * .05;
	}

	public int getYear()
	{//adds year to subclass items
		return dvdYear;
	}
	
}//end subclass


Was This Post Helpful? 0
  • +
  • -

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Issues with Exception in thread "main" java.lang.NullPointer

Posted 01 May 2009 - 12:30 AM

edit: actually forget about this

This post has been edited by mostyfriedman: 01 May 2009 - 12:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 Grayton1972  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-April 09

Re: Issues with Exception in thread "main" java.lang.NullPointer

Posted 01 May 2009 - 12:37 AM

View Postmostyfriedman, on 30 Apr, 2009 - 11:30 PM, said:

first of all, you got a problem with your referencing
NewDVD dvd = null;
		double totalDVDprice = 0; //initiallize dvd price to 0

						//item, name, # in stock, price, year
		dvd = new NewDVD (7634, "Harry Potter",22, 20.99, 2006);//year not accurate
		arrayDVD[0] = dvd;
		dvd = new NewDVD (987, "Armageddon",18, 12.95, 2000);//year not accurate
		arrayDVD[1] = dvd;
		dvd = new NewDVD (872643, "Jurassic Park", 15, 13.89, 1990);//year not accurate
		arrayDVD[2] = dvd;


you will end up having all the elements in the array having the same value, because all the elements are referring to dvd and whenever dvd changes, all the elements referring to it will also change

no the rest of my program works great. i am just having issues with the gui coding giving me an error

Exception in thread "main" java.lang.NullPointerException
at GUI.updateFields(InventoryGUI.java:157)
at GUI.<init>(InventoryGUI.java:111)
at InventoryPart1.main(InventoryPart1.java:37)
Java Result: 1
Was This Post Helpful? 0
  • +
  • -

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Issues with Exception in thread "main" java.lang.NullPointer

Posted 01 May 2009 - 12:40 AM

yea yea , i missread the code, sorry about that
Was This Post Helpful? 0
  • +
  • -

#7 Grayton1972  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-April 09

Re: Issues with Exception in thread "main" java.lang.NullPointer

Posted 01 May 2009 - 01:28 AM

help with this please i am running out of time
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Issues with Exception in thread "main" java.lang.NullPointer

Posted 01 May 2009 - 09:12 PM

Java told you:

null pointer exception at line 157
line 157 is:

jguiDvdRestock.setText( "" + String.format("%.2f",inventory[ index ].getdvdRestock());

if you search in your code, you'll see that jguiDvdRestock is not initialized
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1