Results 1 to 9 of 9
  1. #1
    sylo18 is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default Name Coming Up As "null"

    hey all 1st post so sorry if i dont give all the right info etc.
    Basically im doing a course at UNI (Digital Media) but first year all computing classes do the same basic stuff, incase you want to change course to something else in computing..

    anyway, part of it is Java and im pretty average at it, but im stuck on something and thought you guys could help.

    I started with this code:

    Java Code:
    /**
     * @author (Zaid Alkayat) 
     * @version (1.0)
     */
    public class MyPrinter
    {
        // Number of prints made
        private int copies;
        // Number of sheets of paper avaliable
        private int paper;
        // Name of the printer
        private String name;
    
        /**
         * Constructor for objects of class MyPrinter
         */
        public MyPrinter()
        {
            copies = 0;
            paper = 500;
            name = "Epson";
        }
    
        public int getCopies()
        {
            return copies;
    
        }   
        
        public int getPaper()
        {
            return paper;
    
        }
        
        	public String getName()
    	{
    		return name;
    
    	}
    	
    	public void testMyPrinter()
    	{
    	System.out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    	System.out.println("0123456789");
    	}
    
    }
    and was told to do this to it:
    Edit the MyPrinter class by
    making a copy of the Constructor and editing it so that it has the signature
    public MyPrinter(String name, int startPaper)
    and initialises the values of the printername and the number of sheets of paper to the values of the parameters passed (the number of copies made obviously stays as 0 for a new printer.

    MyPrinter needs some printing methods. The first, signature:
    public boolean printOne(String text) {}
    should take a String as a parameter, and output it on a single line to the terminal window.
    It should also increment (add 1 to) the total number of copies made, and decrement (subtract 1 from) the number of sheets of paper available.
    It should return the boolean value true(this is actually to avoid confusion in the next assignment)

    the second, signature:
    public void print5(String text) {}
    should take a String as a parameter and outputs it on five successive single lines to the terminal window.
    It should also increase the total number of copies made by 5, and reduce the number of sheets available by 5.
    It should not return anything.

    so i ended up with this code:

    Java Code:
    /**
     * MyPrinter.
     * 
     * @author (Zaid Alkayat) 
     * @version (1.0)
     */
    public class MyPrinter
    {
        // Number of prints made
        private int copies;
        // Number of sheets of paper avaliable
        private int paper;
        // Name of the printer
        private String name;
    
        /**
         * Constructor for objects of class MyPrinter
         */
        public MyPrinter()
        {
            copies = 0;
            paper = 500;
            name = "Epson";
        }
         
        public MyPrinter(String name, int startPaper)
        {
            copies = 0;
            paper = 200;
            name = "HP";
        }
        
        public int getCopies()
        {
            return copies;
    
        }   
        
        public int getPaper()
        {
            return paper;
    
        }
        
            public String getName()
        {
            return name;
    
        }
        
        
        public void testMyPrinter()
        {
        System.out.println("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        System.out.println("0123456789");
        }
    
        public boolean printOne(String text)
        {
            boolean success = true;
            {
                System.out.println("Test printOne");
                copies = copies + 1;
                paper = paper - 1;
            }                        
            return success;
        }
    
        public void print5(String text) 
        {
            System.out.println("Test print5");
            System.out.println("Test print5");
            System.out.println("Test print5");
            System.out.println("Test print5");
            System.out.println("Test print5");
            copies = copies + 5;
            paper = paper - 5;
        }
        
    }
    However when i test it, my second Printer name comes up "Null"
    its ment to display:
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    0123456789
    Printer Name: Epson
    Sheets: 500
    copies: 0
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    0123456789
    Printer Name: HP
    Sheets: 200
    copies: 0
    Test printOne
    Sheets: 199
    copies: 1
    Test print5
    Test print5
    Test print5
    Test print5
    Test print5
    Sheets: 194
    copies: 6

    But like i said, instead of HP it comes up null.
    any help would be awesome. THANKS!

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    What are/is the code(s) you used to create and call these classes? (your main method?)

  3. #3
    sylo18 is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default

    this what you mean?
    Java Code:
    public class Test
    {
        public Test()
        {
         MyPrinter p = new MyPrinter();
         p.testMyPrinter();
         System.out.println("Printer Name: " + p.getName() );
         System.out.println("Sheets: " + p.getPaper() );
         System.out.println("copies: " + p.getCopies() );
         p = new MyPrinter("HP", 200); p.testMyPrinter();
         System.out.println("Printer Name: " + p.getName() );
        System.out.println("Sheets: " + p.getPaper() );
        System.out.println("copies: " + p.getCopies() );
        p.printOne("Test printOne"); // ignore return value 
        System.out.println("Sheets: " + p.getPaper() );  
        System.out.println("copies: " + p.getCopies() ); 
        p.print5("Test print5"); // ignore return value
        System.out.println("Sheets: " + p.getPaper() ); 
        System.out.println("copies: " + p.getCopies() );   
        }
    }

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Your code looks correct. Pls paste whole output of your program when you try to run it. Also check if the compiler gives any errors or warning while you try to compile it..

  5. #5
    sylo18 is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default

    i get no errors when compiling
    heres my end result

    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    0123456789
    Printer Name: Epson
    Sheets: 500
    copies: 0
    ABCDEFGHIJKLMNOPQRSTUVWXYZ
    0123456789
    Printer Name: null
    Sheets: 200
    copies: 0
    Test printOne
    Sheets: 199
    copies: 1
    Test print5
    Test print5
    Test print5
    Test print5
    Test print5
    Sheets: 194
    copies: 6

  6. #6
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    Since the string that you passed into the constructor is called 'name', when you say name = "HP" you're changing the String that was passed in. The name attribute of the class is never initialized which is way you are getting null.

    Change it to:

    Java Code:
       public MyPrinter(String name, int startPaper)
        {
            copies = 0;
            this.paper = startPaper;
            this.name = name;
        }
    Then when you call the constructor:

    Java Code:
    MyPrinter foo = new MyPrinter("HP", 200);
    Give that a try.

    They shouldn't be too tough. If you can't figure it out, put another post up.
    Last edited by ShoeNinja; 10-30-2007 at 11:32 PM.

  7. #7
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    That is strange. It should not do that. Make sure that you recompiled MyPrinter again, before running it. I guess you are running the program with a previous version of this class.

  8. #8
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    Since the string that you passed into the constructor is called 'name', when you say name = "HP" you're changing the String that was passed in. The name attribute of the class is never initialized which is way you are getting null.
    Good point! This is exactly the problem. I don't know how i missed that. :(

  9. #9
    sylo18 is offline Member
    Join Date
    Oct 2007
    Posts
    11
    Rep Power
    0

    Default

    dude THANK YOU SOOOOO MUCH!!

    iv got one more part to this task but we start it 2moz, hopefully i can do that as this was the first time i was really stuck.

    but you guys are awesome.. i now love this site <3 :o

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 10:26 AM
  2. Why does it still have it as "Null"?
    By cart1443 in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 06:56 AM
  3. Replies: 1
    Last Post: 07-27-2007, 11:59 PM
  4. Replies: 1
    Last Post: 07-25-2007, 11:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •