Results 1 to 8 of 8
Thread: Keeps returning null
- 02-22-2009, 08:01 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
Keeps returning null
Ok, I'm going to try an explain this the best I can. I have two classes, here is one(The only important ones are the RationalNumber(int) constructor and add(int) method):
And here is my problem, I have instantiated num1, num2 and num3 as RationalNumbers,Java Code:import java.util.*; class RationalNumber { private int numerator, denominator; //No args constructor public RationalNumber() { System.out.println("No Args Constructor"); numerator = 0; } //int arg constructor public RationalNumber(int num1) { System.out.println("num1 is " + num1); numerator = num1; } //int 2 args constructor public RationalNumber(int num1, int num2) { numerator = num1; denominator = num2; } public int getNumerator() { return numerator; } public int getDenominator() { return denominator; } public RationalNumber add(RationalNumber num1) { RationalNumber i = new RationalNumber(numerator + num1.getNumerator()); System.out.println("i is " + i); return i; } public RationalNumber sub(RationalNumber num1) { System.out.println("Subtracting: " + numerator + " - " + num1.getNumerator()); int j = numerator - num1.getNumerator(); RationalNumber i = new RationalNumber(j); return i; } public RationalNumber mlt(RationalNumber num1) { return num1; } public RationalNumber div(RationalNumber num1) { return num1; } public boolean lt(RationalNumber num1) { return (numerator < num1.getNumerator()); } public boolean gt(RationalNumber num1) { return (numerator > num1.getNumerator()); } public boolean eq(RationalNumber num1) { return (numerator == num1.getNumerator()); } public boolean le(RationalNumber num1) { return (numerator <= num1.getNumerator()); } public boolean ge(RationalNumber num1) { return (numerator >= num1.getNumerator()); } public String toString() { return null; } public boolean equals(Object obj) { return false; } }
I set them all to null initially,
then I put the two numbers in num1 and num2 and call num3 = num1.add(num2)
My result is null, why isn't it returning the answer? I have some print lines in there as you can see. The answer is correct all the way until it returns from the single int constructor to the add method, in the constructor the argument passed is the answer, but when it gets back to the add method it is null??? I can post the other class as well if needed.
- 02-22-2009, 09:24 PM #2
i don't see anything wrong w/ your add method, so how are you using your class? are you trying to print it in a print statement? if so, take a look at your toString method, it returns NULL!!!!
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-22-2009, 09:44 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
I'm not using the toString method, here is the main, the switch statement contains the code that is resulting in the problem.
Java Code:import java.io.*; import java.util.*; class proj1 { private BufferedReader inputFile; private PrintWriter outputFile; private int totalInputcount, goodInputCount, badinputCount; private boolean kbInput, fileInput; public void processData(String inLine) { StringTokenizer st = new StringTokenizer(inLine); String s1 = null, s2 = null, s3 = null; RationalNumber num1 = null, num2 = null, num3 = null; String op = null, outString = "No Errors"; try { if (st.hasMoreTokens()) s1 = st.nextToken(); if (st.hasMoreTokens()) s2 = st.nextToken(); if (st.hasMoreTokens()) s3 = st.nextToken(); num1 = new RationalNumber(Integer.parseInt(s1)); num2 = new RationalNumber(Integer.parseInt(s3)); op = s2; num3 = null; switch(op.charAt(0)) { case '+': num3 = num1.add(num2); outString = inLine + " = " + num3; break; case '<': if(op.equals("<=")) { outString = inLine + " = " + num1.le(num2); } else { outString = inLine + " = " + num1.lt(num2); } //outString = line + op; break; case '-': num3 = num1.sub(num2); outString = inLine + " = " + num3; break; default: throw new NoSuchElementException(); } } catch(NoSuchElementException e) { outString = "Invalid Operand " + op + " in the input " + inLine; } catch(NullPointerException e) { outString = "Invalid input " + inLine; } finally { System.out.println(outString); } } public void printBanner() { System.out.println("*****************************************************"); System.out.println("****************Welcome to Project 1*****************"); System.out.println("*****************************************************"); } public void printSummary() { } public void closeFiles() { } public void run(String[] args) { } public static void main(String[] args) { proj1 pl = new proj1(); pl.processData("2 + 4"); pl.processData("2 < 4"); pl.processData("2 <= 1"); } }
- 02-22-2009, 10:10 PM #4
its toString(), its autocalled. try chainging it to
public String toString()
{
return "XYZ";
}
you'll get XYZ instead of nullUSE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-22-2009, 10:25 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
That's awesome, but how do I fix it from being "autocalled?"
- 02-22-2009, 11:23 PM #6
don't put a toString() method in there in the first place. if you must, return something. and use num1.getNumberator() instead of num1.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-23-2009, 01:58 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
- 02-23-2009, 02:25 AM #8
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
hmmmm, I'm in quite a bind here then. This is actually a project for my Java class and the toString method is a requirement. There isn't any way to keep it from being called? I guess I will remove it for now, at least until I can find a way to avoid autocalling it.
Edit: I removed the toString and now it's returning what looks to be the address of the memory location... I get this: "RationalNumber@19821f" in the outstring where num3 should be.
Last Edit: OK, I was able to get around it by doing this, if it's going to run anyway, make it work when it runs right?:Thanks for all your help.Java Code:public String toString() { String outString; outString = "" + numerator; return outString; }Last edited by ribbs2521; 02-23-2009 at 02:34 AM.
Similar Threads
-
returning arrays
By cjohnson412 in forum New To JavaReplies: 4Last Post: 11-25-2008, 01:30 PM -
Why is my list returning nothing?
By xcallmejudasx in forum New To JavaReplies: 2Last Post: 11-05-2008, 03:51 PM -
String returning null value
By impact in forum New To JavaReplies: 7Last Post: 08-03-2008, 07:49 AM -
returning a value from an arraylist
By xkross in forum New To JavaReplies: 2Last Post: 04-18-2008, 05:30 PM -
Need help returning data from database
By dyn03 in forum JDBCReplies: 0Last Post: 03-11-2008, 04:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks