Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Norm

Thread: Why is there null?

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Why is there null?

    hey folks... why does the method below return me nulls. i.e. it skips some objects?
    Java Code:
    public static Exam[] collateExams(Exam[] exams)
    	{
    	
    		Exam[] r = new Exam[exams.length]; 
    		int index = 0;
    		
    		for (int i = 0; (i < exams.length) && (index < exams.length/2); i++)
    		{
    			if (exams[i].getExamType() == 'm')
    			{
    				r[index*2] = new Exam(exams[i].getFirstName(), exams[i].getLastName(), exams[i].getID(), "midterm", exams[i].getScore());
    				for(int j = 0; (j < exams.length) && (index < exams.length/2); j++)
    				{
    					if((exams[j].getExamType() == 'f') && (exams[i].getID() == exams[j].getID()))
    					{
    						r[index*2 + 1] = new Exam(exams[i].getFirstName(), exams[i].getLastName(), exams[i].getID(), "final", exams[i].getScore());
    					}
    				}
    			}
    			index++;
    		}
    		return r;
    	}
    My output from this method is :
    Java Code:
    null
    null
    Bill Gates 6 midterm  90
    Bill Gates 6 final  90
    James Gosling 3 midterm  100
    James Gosling 3 final  100
    Sergey Brin 22 midterm  98
    null
    Dennis Ritchie 5 midterm  94
    Dennis Ritchie 5 final  94
    Steve Jobs 9 midterm  95
    Steve Jobs 9 final  95

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Why is there null?

    null would mean that no value had been assigned to those variables.
    ssk2659 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Why is there null?

    These questions are easily answered by you yourself when you take a little time to add some System.out.println() statements to the code to see what it is doing. Right now you're just staring at code with a particular expectation that is not coming true; you have to pinpoint where in the code what you expects breaks from reality. A good place to start is to make the code spit out what it is not doing:

    Java Code:
    public static Exam[] collateExams(Exam[] exams)
        {
         
            Exam[] r = new Exam[exams.length];
            int index = 0;
             
            for (int i = 0; (i < exams.length) && (index < exams.length/2); i++)
            {
                if (exams[i].getExamType() == 'm')
                {
                    System.out.println("EXAMS[" + i + "] IS OF TYPE 'M'; INDEX IS '" + index + "'");
      
                    r[index*2] = new Exam(exams[i].getFirstName(), exams[i].getLastName(), exams[i].getID(), "midterm", exams[i].getScore());
                    for(int j = 0; (j < exams.length) && (index < exams.length/2); j++)
                    {
                        if((exams[j].getExamType() == 'f') && (exams[i].getID() == exams[j].getID()))
                        {
                            r[index*2 + 1] = new Exam(exams[i].getFirstName(), exams[i].getLastName(), exams[i].getID(), "final", exams[i].getScore());
                        }
                    }
                } else {
    
                    System.out.println("EXAMS[" + i + "] WAS NOT OF TYPE 'M'; INDEX IS '"  + index + "'");
                }
                index++;
            }
            return r;
        }
    Notice how I added the 'else' block to make visual where the type of the exam was not 'm' and the code basically does nothing. You can add more of such print statements to the code to print out more and more information; vital information to be able to see what is wrong rather than guess.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Why I'm getting NULL
    By amrmb09 in forum Advanced Java
    Replies: 23
    Last Post: 11-09-2011, 12:54 AM
  2. Replies: 6
    Last Post: 10-18-2010, 10:40 AM
  3. Null :(
    By PhQ in forum Advanced Java
    Replies: 4
    Last Post: 04-12-2010, 01:05 AM
  4. Replies: 2
    Last Post: 03-19-2010, 05:30 PM
  5. What is NULL
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-09-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
  •