Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-30-2007, 09:45 PM
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
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:

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:

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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-30-2007, 10:19 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
What are/is the code(s) you used to create and call these classes? (your main method?)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 10-30-2007, 10:59 PM
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
this what you mean?
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() ); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 10-30-2007, 11:06 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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..
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 10-30-2007, 11:19 PM
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 10-30-2007, 11:26 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
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:

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

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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 10-30-2007, 11:29 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 10-30-2007, 11:30 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 10-30-2007, 11:31 PM
Member
 
Join Date: Oct 2007
Posts: 11
sylo18 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does it still have it as "Null"? cart1443 New To Java 1 02-07-2008 06:56 AM
Hwlp with "Open", "Save", "Save as..." trill New To Java 1 07-31-2007 08:53 AM
Exception in thread "main" java.net.ConnectException: Connection timed out osval Advanced Java 1 07-27-2007 11:59 PM
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException romina New To Java 1 07-25-2007 11:55 PM
ArrayList: Exception in thread "main" java.lang.NullPointerException susan New To Java 1 07-16-2007 07:32 AM


All times are GMT +3. The time now is 10:37 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org