10 Replies - 452 Views - Last Post: 05 December 2011 - 08:15 PM Rate Topic: -----

#1 cggalvin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-December 11

Confused on why this snippet errors.

Posted 05 December 2011 - 01:24 PM

Hey guys,
Just a quick question, probably has a simple answer.

I can't seem to get my head around why this code errors?

 case 4:
                    System.out.print("Enter student name: ");
                    String searchstu = scan.next();
                    searchstu = searchstu.toLowerCase();
                    for (int l = 0; l < STU.length; l++) {
                        if (searchstu.equals(STU[l].getStudentName())) {
                            for (int m = 0; m < ATC.length; m++) {
                                stu = ATC[m].getStudent();
                                if (STU[l].equals(stu)) {
                                    System.out.println(ATC[m].getModule());
                                }
                            }
                        } else {
                            System.out.println("Please use a valid student name");
                        }
                    }
                    break;



When I input a valid student name, the program will output the correct output but is followed by an error;

Exception in thread "main" java.lang.NullPointerException



Any help, is much appreciated.
Daniel

Is This A Good Question/Topic? 0
  • +

Replies To: Confused on why this snippet errors.

#2 TheCompBoy  Icon User is offline

  • D.I.C Regular

Reputation: 11
  • View blog
  • Posts: 314
  • Joined: 21-April 11

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 01:29 PM

Could you post mthe whole error log so we can see which line caused the error..?
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7573
  • View blog
  • Posts: 12,724
  • Joined: 19-March 11

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 01:35 PM

What's probably happening is this:

you've declared an array of length l, and you've put some Students into it, but the number of students you've put in is less than l.

Your loop doesn't exit after you find a student, so it keeps going to the end of the loop - or it tries to.
Eventually, it gets to this line:

if (searchstu.equals(STU[l].getStudentName())) {


but for STU[l] at that value of l, there's a null object. Hence, you get a null pointer exception.

Two things you can do here. One would be to check for null object before you try to dereference, the other would be to break when you find what you're looking for. Both would be useful.
Was This Post Helpful? 1
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 01:36 PM

Probably is because your array is not all filled, that when it tries to get the other index values they are null. So you have to make sure you only go to the maximum available data and not the total size because some array indexes are empty.
Was This Post Helpful? 0
  • +
  • -

#5 cggalvin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-December 11

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 01:49 PM

Thanks for the quick replies guys.

The line that causes the problem is;

stu = ATC[m].getStudent();



Both arrays are full, ie; Array of Students is populated fully and Array of Modules is populated fully.

Could it be that my Attachment array (ATC) isn't fully populated?
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7573
  • View blog
  • Posts: 12,724
  • Joined: 19-March 11

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 01:58 PM

Quote

Could it be that my Attachment array (ATC) isn't fully populated?


Yep. Same explanation as before, except with the other array - you're going all the way through ATC, and getting to a null object. Same fixes as before, as well: check your comparison objects to make sure they exist, and break when you find what you're looking for.
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 02:04 PM

That means so, that your ATC is not full populated and so you cant go to the last index asking to getStudent at a null value index. As I said, if your array has 10 size, and you only added 3 values, then you have to go to only three values and not the whole array.
Was This Post Helpful? 0
  • +
  • -

#8 cggalvin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-December 11

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 02:06 PM

Cheers for the help, how would i go about testing if the array contains null objects and stopping the search through?

Again, any help is always appreciated.
Daniel
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 02:21 PM

The simplest way is to have a count of how many elements are in array and so instead of looping through the whole array, we loop to the count.
This means we need to have account that increment at the method which adds element to that array.
Was This Post Helpful? 0
  • +
  • -

#10 cggalvin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 01-December 11

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 02:32 PM

Ahh, ok i see what you mean, I'm unsure how to implement this, could you give me any examples in which I can expand on?

So what i need to do is;

Search the array to find the first null object, move back one place in the array and break the search code?
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7573
  • View blog
  • Posts: 12,724
  • Joined: 19-March 11

Re: Confused on why this snippet errors.

Posted 05 December 2011 - 08:15 PM

There are a couple of things you could do.

One would be to have your if statement read like this:
if (STU[l]!=null && searchstu.equals(STU[l].getStudentName()))


This makes use of lazy evaluation: if the first clause if false, the whole clause is false, so you don't evaluate the second clause. This is exactly what we want here, of course, because if the first clause is false, then STU[l] is null, and you don't want to try to valuate the clause that references it as an object.

This would run with your code as it is, you wouldn't need to change anything else. It would be useful to put a break statement in here:

 if (STU[l].equals(stu)) {
   System.out.println(ATC[m].getModule());
   break;
   }



because at this point you know you've found your match. This means that you don't need to look any more, which means you save a lot of operations that you don't need to do.

You might do the first if a little differently:

if (STU[l]!=null) {
    break;
  }
else if (searchstu.equals(STU[l].getStudentName()))


This, again, gets you out of the loop when you know you're done.

Another way you could do this would be, as smohd suggests, to keep track of how many students are actually in each array, and run your loop out to that point, rather than out to array.length
This is okay if you're not changing that array too much - if you're adding and deleting students, it's too easy to lose track. There's an opportunity for a very annoying bug, for example you might lose track of the last student in the list - this is annoying because it's only going to turn up in some cases, and you have to write a special case to test for it, and ugh. Bothersome.

Another thing, again if the list isn't very dynamic, would be to simply ensure that the array is exactly as long as the list of students. Don't try to add students, though.


Or, you could consider the arraylist, which is a structure well suited to this situation. You can access its members much like an array, but it's dynamic and it keeps track of how many members it has for you. There's a reason we use a lot of arraylists in day-to-day programming. They're very convenient.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1