Results 1 to 8 of 8
  1. #1
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default 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):
    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;
      }
    
    
    }
    And here is my problem, I have instantiated num1, num2 and num3 as RationalNumbers,
    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.

  2. #2
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  3. #3
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    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");
        }
    
    }

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    its toString(), its autocalled. try chainging it to
    public String toString()
    {
    return "XYZ";
    }

    you'll get XYZ instead of null
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    That's awesome, but how do I fix it from being "autocalled?"

  6. #6
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by ribbs2521 View Post
    That's awesome, but how do I fix it from being "autocalled?"
    You should realise that these lines

    Java Code:
    num3 = num1.add(num2);
    outString = inLine + " = " + num3;
    are what are causing toString() to be called. In particular when you say "+num3" then num3.getString() gets concatenated on to outString.

  8. #8
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    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?:
    Java Code:
      public String toString()
      {
     	  String outString;
     	  outString = "" + numerator;
    	  return outString;
      }
    Thanks for all your help.
    Last edited by ribbs2521; 02-23-2009 at 02:34 AM.

Similar Threads

  1. returning arrays
    By cjohnson412 in forum New To Java
    Replies: 4
    Last Post: 11-25-2008, 01:30 PM
  2. Why is my list returning nothing?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 03:51 PM
  3. String returning null value
    By impact in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 07:49 AM
  4. returning a value from an arraylist
    By xkross in forum New To Java
    Replies: 2
    Last Post: 04-18-2008, 05:30 PM
  5. Replies: 0
    Last Post: 03-11-2008, 04:55 PM

Posting Permissions

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