Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Array help

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array help

    Hi, i am having trouble getting the following String array to run correctly and having compile error. My compile error is posted below.



    stapler.java:94: error: cannot find symbol
    System.out.println("Product name: " +stapler.getnames[0]);
    ^
    symbol: variable getnames
    location: variable stapler of type Stapler
    stapler.java:105: error: cannot find symbol
    System.out.println("Product name: " +stapler1.getnames[1]);
    ^
    symbol: variable getnames
    location: variable stapler1 of type Stapler
    stapler.java:116: error: cannot find symbol
    System.out.println("Product name: " +stapler2.getnames[2]);
    ^
    symbol: variable getnames
    location: variable stapler2 of type Stapler
    3 errors







     
     
    public class Stapler {
     
    		String[] name;
    		int numberItem;
    		double inStock;
    		double price;
     
     
    		int numberItem1;
    		double inStock1;
    		double price1;
     
     
    		int numberItem2;
    		double inStock2;
    		double price2;
     
    			public String[] getnames(){
    			return(name);
    			}
    				public int getItemnumber(){
    				return(numberItem);
    				}
    					public double getprices(){
    					return(price);
    					}
    						public double getunits(){
    						return(inStock);
    						}	
    							public double getVolume() {
    							return (inStock * price);
    							}
     
    				public int getItemnumber1(){
    				return(numberItem1);
    				}
    					public double getprices1(){
    					return(price1);
    					}
    						public double getunits1(){
    						return(inStock1);
    						}	
    							public double getVolume1() {
    							return (inStock1 * price1);
    							}
     
     
     
    				public int getItemnumber2(){
    				return(numberItem2);
    				}
    					public double getprices2(){
    					return(price2);
    					}
    						public double getunits2(){
    						return(inStock2);
    						}	
    							public double getVolume2() {
    							return (inStock2 * price2);
    							}			
     
     
    	Stapler(){
    		String[] name = {"Miller's Stapler", "Miller's Staples", "Miller's Paper"};
    		numberItem = 123456;
    		inStock = 123;
    		price = 4.75;
     
     
    		numberItem1 = 654321;
    		inStock1 = 321;
    		price1 = 2.50;
     
     
     
    		numberItem2 = 987654;
    		inStock2 = 987;
    		price2 = 1.37;
    		}
     
     
     
    	public static void main(String[] args) {
    		Stapler stapler, stapler1, stapler2;
     
    			stapler = new Stapler();
     
    			stapler1 = new Stapler();
     
    			stapler2 = new Stapler();
     
    	System.out.println("Product name: " +stapler.getnames[0]);
     
    	System.out.println("Product Number: "+stapler.getItemnumber());
     
    	System.out.println("Price of one product: "+stapler.getprices());
     
    	System.out.println("Number of units in stock: "+stapler.getunits());	
     
    	System.out.println("Total value of Staplers is: "+stapler.getVolume());
    	System.out.println();
    	//
    	System.out.println("Product name: " +stapler1.getnames[1]);
     
    	System.out.println("Product Number: "+stapler1.getItemnumber1());
     
    	System.out.println("Price of one product: "+stapler1.getprices1());
     
    	System.out.println("Number of units in stock: "+stapler1.getunits1());	
     
    	System.out.println("Total value of Staplers is: "+stapler1.getVolume1());
    	System.out.println();
    	//
    	System.out.println("Product name: " +stapler2.getnames[2]);
     
    	System.out.println("Product Number: "+stapler2.getItemnumber2());
     
    	System.out.println("Price of one product: "+stapler2.getprices2());
     
    	System.out.println("Number of units in stock: "+stapler2.getunits2());	
     
    	System.out.println("Total value of Staplers is: "+stapler2.getVolume2());
    	System.out.println();
    	}
    }


  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array help

    Hi,

    Could you please see the below syntax.

    System.out.println("Product name: " +stapler.getnames()[0]);

    Regards
    Lakshmi M

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array help

    This is my new code:
     
    public class Stapler {
     
    		String[] name;
    		int[] numberItem;
    		double[] inStock;
    		double[] price;
     
     
    			public String[] getnames(){
    			return(name);
    			}
    				public int[] getItemnumber(){
    				return(numberItem);
    				}
    					public double[] getprices(){
    					return(price);
    					}
    						public double[] getunits(){
    						return(inStock);
    						}	
    							public double getVolume() {
    							return (inStock[0] * price[0]);
    							}
     
     
     
     
    							public double getVolume1() {
    							return (inStock[1] * price[1]);
    							}
     
     
    							public double getVolume2() {
    							return (inStock[2] * price[2]);
    							}	
     
    			public double getTotal() {
    			return (price[2]*inStock[2]+price[1]*inStock[1]+price[0]*inStock[0]);
    			}
     
     
    	Stapler(){
    		String[] name = {"Miller's Stapler", "Miller's Staples", "Miller's Paper"};
    		int[] numberItem = {123456, 654321, 987654};
    		int[] inStock = {123, 321, 987};
    		double[] price = {4.75, 2.50, 1.37};
     
    		}
     
     
     
    	public static void main(String[] args) {
    		Stapler stapler, stapler1, stapler2;
     
    			stapler = new Stapler();
     
    			stapler1 = new Stapler();
     
    			stapler2 = new Stapler();
     
     
     
     
    	System.out.println("Product name: " +stapler.getnames()[0]);
     
    	System.out.println("Product Number: " +stapler.getItemnumber()[0]);
     
    	System.out.println("Price of one product: "+stapler.getprices()[0]);
     
    	System.out.println("Number of units in stock: "+stapler.getunits()[0]);	
     
    	System.out.println("Total value of Staplers is: "+stapler.getVolume());
    	System.out.println();
    	//
    	System.out.println("Product name: " +stapler1.getnames()[1]);
     
    	System.out.println("Product Number: "+stapler1.getItemnumber()[1]);
     
    	System.out.println("Price of one product: "+stapler1.getprices()[1]);
     
    	System.out.println("Number of units in stock: "+stapler1.getunits()[1]);	
     
    	System.out.println("Total value of Staplers is: "+stapler1.getVolume1());
    	System.out.println();
    	//
    	System.out.println("Product name: " +stapler2.getnames()[2]);
     
    	System.out.println("Product Number: "+stapler2.getItemnumber()[2]);
     
    	System.out.println("Price of one product: "+stapler2.getprices()[2]);
     
    	System.out.println("Number of units in stock: "+stapler2.getunits()[2]);	
     
    	System.out.println("Total value of Staplers is: "+stapler2.getVolume2());
    	System.out.println();
     
    	System.out.println("Total value of all inventory is: "+stapler.getTotal());
     
    	}
    }
    I get this error when i run it:
    Exception in thread "main" java.lang.NullPointerException
    at Stapler.main(stapler.java:67)
    How do i fix it.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Array help

    Hi, you are not assigned the values to instance variables in 'no argument constructor'. Please assing the values to instance variable in no argument construcor like

    Stapler() {
    name = {"Miller's Stapler", "Miller's Staples", "Miller's Paper"}
    numberItem = {123456, 654321, 987654};
    inStock = {123, 321, 987};
    price = {4.75, 2.50, 1.37};
    }

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array help

    The constructor is already assinged those variables. so you pretty much told me to change nothing and it would work. That is not helpful.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array help

    Exception in thread "main" java.lang.NullPointerException
    at Stapler.main(stapler.java:67)
    Line 67 had a variable with a null value when it was executed. Look at line 67, find the variable with the null value and backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    47
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array help

    I cannot find any variable that could cause it to be a null value. I have checked all the code and nothing is jumping out at me. Could it be wrong placment of code?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Array help

    If you can not see what variable online 67 is null, let the computer tell you.
    Add a println statement just before statement 67 that prints out the values of all the variables used on line 67. The print out will show you which variable is null.

    Could it be wrong placment of code?
    Yes. The program is using a variable that has not been given a value and is still null.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  2. [SOLVED] Merge two 1D array's into a new 1D array. Trouble appending last index
    By norske_lab in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 30th, 2012, 12:57 PM
  3. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  4. Replies: 6
    Last Post: October 7th, 2011, 07:50 AM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM