14 Replies - 808 Views - Last Post: 08 May 2012 - 07:29 PM Rate Topic: -----

#1 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 06:34 PM

Exception in thread "main" java.lang.NullPointerException
at Inventory.main(Inventory.java:86)

Any ideas on how to fix this?
 import java.util.*;
	 
	    class Camera {
	     
	        private String ProductName;
	        private int ItemNumber;
	        private int NumberofUnits;
	        private double UnitPrice;
	     
	         public Camera(String Name, int Number, int Units, double Price) {
	 
	 
	             ProductName = Name;
	             ItemNumber = Number;
	             NumberofUnits = Units;
	             UnitPrice = Price;
	          }
	     
	           public void setProductName(String title, String Name)
	          {
	                ProductName = Name;
	          }
	 
	          public String getProductName() {
	               return ProductName;
	         }
	 
	         public void setItemNumber(int Number) {
	               ItemNumber = Number;
	         }
	 
	         public double getItemNumber() {
	              return ItemNumber;
	        }
	 
	         public void setNumberofUnits(int Units) {
	              NumberofUnits = Units;
	        }
	 
	         public double getNumberofUnits() {
	              return NumberofUnits;
	        }
	 
	         public void setUnitPrice(double Price) {
	              UnitPrice = Price;
	        }
	 
	         public double getUnitPrice() {
	              return UnitPrice;
	        }
	 
	         public double value() {
	             return UnitPrice * NumberofUnits;
	       }
	    }
	 
	 
	
	public class Inventory
	{
	    private static final Camera[] Camera = null;



		private static Scanner input = new Scanner(System.in);
	     
                

               Camera array[]  = new Camera[5];{

	               
				Camera[0] = new Camera("Nikon", 0001, 5, 1499);
                       Camera[1] = new Camera("Olympus", 0002, 7, 799);
                       Camera[2] = new Camera("Canon", 0003, 3, 1549);
                       Camera[3] = new Camera("Casio", 0004, 10, 89);
                       Camera[4] = new Camera("Kodak", 0005, 3, 299);

                       }

	        public static void main( String args[])
	       {
	           
	              System.out.println("Camera Inventory Program");
	              Camera[] myCamera = Camera;

	 			  System.out.printf("Product Name is" + myCamera.length);
	              System.out.printf("Item Number is" + myCamera.length );
	              System.out.printf("Number of Units is" + myCamera.length );
	 
	              System.out.printf("Unit Price is" + myCamera.length);
	     
	      }

			public static Scanner getInput() {
				return input;
			}

			public static void setInput(Scanner input) {
				Inventory.input = input;
			}
	   } 
 


Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.NullPointerException

#2 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 06:37 PM

Why are you calling myCamera.length? Wouldnt you want to call
 myCamera.getProductName()
for the first print line at line 86
Was This Post Helpful? 1
  • +
  • -

#3 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 06:39 PM

When I use it in eclipse it brings it up as an error wity myCamera.getProductName()
Was This Post Helpful? 0
  • +
  • -

#4 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 06:46 PM

I think you will want to implement a for loop to print your myCamera data

	for(int i = 0; i < myCamera.length; i ++)
	{
		  System.out.printf("Product Name is" + myCamera[i].length);
          System.out.printf("Item Number is" + myCamera[i].length );
          System.out.printf("Number of Units is" + myCamera[i].length );
          System.out.printf("Unit Price is" + myCamera[i].length);
	}



Sorry those werent supposed to be myCamera.length, rathermyCamera.getProduct() and so on...
Was This Post Helpful? 1
  • +
  • -

#5 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 06:49 PM

ok i am new to this java stuff so any help is appreciated

im getting off of here for tonite been on about 6 hrs working on my program
Was This Post Helpful? 0
  • +
  • -

#6 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 06:49 PM

Also, you declare Camera[] Camera = null, that is why you are getting a null pointer exception because you never give it any data.

This post has been edited by bank217: 07 May 2012 - 06:50 PM

Was This Post Helpful? 1
  • +
  • -

#7 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 06:50 PM

i just started using eclipse and it gave me tht suggestion
Was This Post Helpful? 0
  • +
  • -

#8 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 07:12 PM

Acetown you still here?
Was This Post Helpful? 1
  • +
  • -

#9 pbl  Icon User is offline

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

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

Re: Exception in thread "main" java.lang.NullPointerException

Posted 07 May 2012 - 07:17 PM

Use that syntax, a lot less confusing



              Camera array[]  = {
                  new Camera("Nikon", 0001, 5, 1499),
                  new Camera("Olympus", 0002, 7, 799),
                  new Camera("Canon", 0003, 3, 1549),
                  new Camera("Casio", 0004, 10, 89),
                  new Camera("Kodak", 0005, 3, 299)
              };
 

And note that you never use "array" you only use the variable "camera" which you initialized to null
Was This Post Helpful? 1
  • +
  • -

#10 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 08 May 2012 - 02:52 PM

This is the error I get now.
c:\Program Files\projects>java Inventory
Camera Inventory Program
Exception in thread "main" java.lang.NullPointerException
at Inventory.main(Inventory.java:95)
Here is my code
 import java.util.*;
	 
	    class Camera {
	     
	       
			
	        private String ProductName;
	        private int ItemNumber;
	        private int NumberofUnits;
	        private double UnitPrice;
	     
	         public Camera(String Name, int Number, int Units, double Price) {
	 
	 
	             ProductName = Name;
	             ItemNumber = Number;
	             NumberofUnits = Units;
	             UnitPrice = Price;
	          }
	     
	           public void setProductName(String title, String Name)
	          {
	                ProductName = Name;
	          }
	 
	          public String getProductName() {
	               return ProductName;
	         }
	 
	         public void setItemNumber(int Number) {
	               ItemNumber = Number;
	         }
	 
	         public double getItemNumber() {
	              return ItemNumber;
	        }
	 
	         public void setNumberofUnits(int Units) {
	              NumberofUnits = Units;
	        }
	 
	         public double getNumberofUnits() {
	              return NumberofUnits;
	        }
	 
	         public void setUnitPrice(double Price) {
	              UnitPrice = Price;
	        }
	 
	         public double getUnitPrice() {
	              return UnitPrice;
	        }
	 
	         public double value() {
	             return UnitPrice * NumberofUnits;
	       }
	    }
	 
	 
	
	public class Inventory
	{
	    

		



		private static Scanner input = new Scanner(System.in);



		private static Camera[] Camera;
	     
                

               Camera array[]  = {

	               
		       new Camera("Nikon", 0001, 5, 1499),
                       new Camera("Olympus", 0002, 7, 799),
                       new Camera("Canon", 0003, 3, 1549),
                       new Camera("Casio", 0004, 10, 89),
                       new Camera("Kodak", 0005, 3, 299),

                       };

	        public static void main( String args[])
	       {
	           
	              System.out.println("Camera Inventory Program");
	             
				Camera[] myCamera = Camera;
				
                      for(int i = 0; i < myCamera.length; i ++)
                      {

		      System.out.printf("Product Name is " + myCamera[i].getProductName());
	 
	              System.out.printf("Item Number is" + myCamera[i].getItemNumber() );
	              System.out.printf("Number of Units is" + myCamera[i].getNumberofUnits() );
	 
	              System.out.printf("Unit Price is" + myCamera[i].getUnitPrice());
	             }
	      }

			public static Scanner getInput() {
				return input;
			}

			public static void setInput(Scanner input) {
				Inventory.input = input;
			}
	   } 
  

Was This Post Helpful? 0
  • +
  • -

#11 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 08 May 2012 - 03:23 PM

This is how I would have written your code...

public class Camera
{

	private String ProductName;
	private int ItemNumber;
	private int NumberofUnits;
	private double UnitPrice;

	public Camera(String Name, int Number, int Units, double Price)
	{

		ProductName = Name;
		ItemNumber = Number;
		NumberofUnits = Units;
		UnitPrice = Price;
	}

	public void setProductName(String title, String Name)
	{
		ProductName = Name;
	}

	public String getProductName()
	{
		return ProductName;
	}

	public void setItemNumber(int Number)
	{
		ItemNumber = Number;
	}

	public double getItemNumber()
	{
		return ItemNumber;
	}

	public void setNumberofUnits(int Units)
	{
		NumberofUnits = Units;
	}

	public double getNumberofUnits()
	{
		return NumberofUnits;
	}

	public void setUnitPrice(double Price)
	{
		UnitPrice = Price;
	}

	public double getUnitPrice()
	{
		return UnitPrice;
	}

	public double value()
	{
		return UnitPrice * NumberofUnits;
	}
}


Was This Post Helpful? 1
  • +
  • -

#12 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 08 May 2012 - 03:28 PM

import java.util.Scanner;

public class Inventory
{

	private static Scanner input = new Scanner(System.in);

	public static void main(String args[])
	{
		Camera[] camera = new Camera[5];

		camera[0] = new Camera("Nikon", 0001, 5, 1499);
		camera[1] = new Camera("Olympus", 0002, 7, 799);
		camera[2] = new Camera("Canon", 0003, 3, 1549);
		camera[3] = new Camera("Casio", 0004, 10, 89);
		camera[4] = new Camera("Kodak", 0005, 3, 299);

		System.out.println("Camera Inventory Program");

		for (int i = 0; i < camera.length; i++)
		{
			System.out.println("Product Name is " + camera[i].getProductName());
			System.out.println("Item Number is " + camera[i].getItemNumber());
			System.out.println("Number of Units is "
					+ camera[i].getNumberofUnits());
			System.out.println("Unit Price is " + camera[i].getUnitPrice());
			System.out.println();
		}

	}

	public static Scanner getInput()
	{
		return input;
	}

	public static void setInput(Scanner input)
	{
		Inventory.input = input;
	}
}


Was This Post Helpful? 1
  • +
  • -

#13 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 08 May 2012 - 03:34 PM

Im not sure why you have
 Camera array[]  = 
it should be
Camera[] myCamera =

Was This Post Helpful? 1
  • +
  • -

#14 acetown  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 58
  • Joined: 06-April 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 08 May 2012 - 07:09 PM

Thank you very much. Java is a little challenging when this is your first time doing it.

Create a method to calculate the value of the entire inventory.
Create another method to sort the array items by the name of the product.

This is what I need next. Any ideas on how to do this.
Was This Post Helpful? 0
  • +
  • -

#15 bank217  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 58
  • Joined: 22-March 12

Re: Exception in thread "main" java.lang.NullPointerException

Posted 08 May 2012 - 07:29 PM

You will need to set up a normal method header. Inside the method header you will want to use a for loop similar to what I showed you above that will call the method getNumberOfUnits().
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1