Print 

Author Topic: Weird Java Programming Error  (Read 1724 times)

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Weird Java Programming Error
« on: March 15, 2008, 12:33:49 AM »
Somehow, I have no clue why, I'm getting a Null Pointer Exception in the following code:

for (int i = 0; i < studentRequest.size(); i++)
   requests.add(studentRequest.get(i));

Both studentRequest and requests are Array Lists. This is just baffling me, since there should be no possible way for there to be a Null Pointer Exception... This is really annoying because this is a very important program for me.
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Offline MrMxyzptlk

  • Posts Too Much
  • *****
  • Posts: 9208
  • Never backward,           always forward!
    • My 5th Dimensional Homepage
Re: Weird Java Programming Error
« Reply #1 on: March 15, 2008, 04:12:05 AM »
Well, this compiled and ran perfectly fine for me:



import java.util.ArrayList;

public class EdiTest
{

  public static void main(String[] argv)
  {

   ArrayList studentRequest = new ArrayList();
   ArrayList requests = new ArrayList();

   studentRequest.add("a");
   studentRequest.add("b");
   studentRequest.add("c");
   studentRequest.add("d");
   studentRequest.add("e");
   
   for (int i = 0; i < studentRequest.size(); i++)
     requests.add(studentRequest.get(i));

   System.out.print("OUTPUT = ");
   for (int i = 0; i < requests.size(); i++)
     System.out.print(requests.get(i));

  }
}


Producing the expected output:

OUTPUT = abcde



Do you perhaps have mismatching types (requiring casting for the "add"s) or possible null values in the studentRequest ArrayList?

Easy enough to check the latter by trying:

   for (int i = 0; i < studentRequest.size(); i++)
   {
     if (studentRequest.get(i) == null)
       System.out.print("studentRequest(" + i + ") has value null");
     else requests.add(studentRequest.get(i));
   }

Then if it's valid to have null values in studentRequest, but not in requests just modify it to make use of the (reversed) if-test for null....


P.S. It should still work even if it has a value of "null," tho.... Are you sure the exception is right at those two lines?? (I tested that by changing the "c" value to null, and it still worked fine....)
« Last Edit: March 15, 2008, 04:23:55 AM by MrMxyzptlk »
Mr. Mxy's current Word Corner word is catachresis    

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Re: Weird Java Programming Error
« Reply #2 on: March 15, 2008, 12:52:31 PM »
I'll show you what happens during debug. There shouldn't be any null values, since I initiate the Array List without a number of spots. When I get right before the part, the size is 6 and the contents of studentRequest are said to be thus:
S09
C09
L09
M09
G09
H09
null
null
null
null

I'm guessing those nulls shouldn't affect it and have something to do with the ArrayList class? Anyways, at this point requests is empty. Stepping once does nothing, then when I do it once more it gives me the exception.

When I print it out instead of adding it to the new Array List, I get this:
S09
C09
L09
M09
G09
H09
and then it exits the for loop, as it should. For some reason, it seems as if the Null Pointer Exception would be coming from the .add(Object obj) method, and not the .get(int index) method. I don't see how you could get that type of exception, though.

I did figure out that I forgot to remove the stuff from studentRequest in the method that calls that, but that shouldn't have an effect.

Hahahaha, I'm an idiot. I never actually initialized requests. Just had ArrayList requests.


You wouldn't happen to know anything about apcslib, would you, Mxy?

Nvm. It looks like I've got nearly everything done with my Class Scheduler except for sorting the students within each class and printing out specific Student or Class Schedules, which will all be easily done. I think I should make more text files, though. Especially ones that test having more students apply to a class than there is space for.
« Last Edit: March 15, 2008, 02:41:08 PM by BFM_Edison »
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Offline MrMxyzptlk

  • Posts Too Much
  • *****
  • Posts: 9208
  • Never backward,           always forward!
    • My 5th Dimensional Homepage
Re: Weird Java Programming Error
« Reply #3 on: March 15, 2008, 11:21:40 PM »
Quote
Hahahaha, [...]. I never actually initialized requests. Just had ArrayList requests.

Shouldn't the Java compiler have caught that?!?  (Or ... do you put -nowarn on the compile directive?  ;)  :siderofl: )

Glad to hear you got it worked out, tho!

GL!
Mr. Mxy's current Word Corner word is catachresis    

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Re: Weird Java Programming Error
« Reply #4 on: March 15, 2008, 11:23:29 PM »
I don't think it would have seen it as a detectable error, since the actual ArrayList had been initialized. Now I've got a fairly pretty 500+ line program :-P It's hard to tell with line counts, though, cus of comments and white spaces and whatnot.
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Offline MrMxyzptlk

  • Posts Too Much
  • *****
  • Posts: 9208
  • Never backward,           always forward!
    • My 5th Dimensional Homepage
Re: Weird Java Programming Error
« Reply #5 on: March 15, 2008, 11:29:02 PM »
I don't think it would have seen it as a detectable error, since the actual ArrayList had been initialized. Now I've got a fairly pretty 500+ line program :-P It's hard to tell with line counts, though, cus of comments and white spaces and whatnot.

...Get a better source editor!  I used to struggle with JBuilder, esp. since I'm a "text editor" sorta Unix crumb from the bygone days.... (FYI: I now use a VERY simple one written by the Prof. that taught the Java class that I attended. It's a 60KB java program that he wrote, and is basically Wordpad with Java smarts, and is very "simple." I.e. Easy to learn and use....

PM me if you want it.... )
Mr. Mxy's current Word Corner word is catachresis    

Offline BFM_Edison

  • Captain
  • *
  • Posts: 3074
Re: Weird Java Programming Error
« Reply #6 on: March 16, 2008, 11:43:30 AM »
Actually, it still should have given me an error about it, since it does that with other objects. And Eclipse is a pretty darn good compiler. Free, too. Now I've got to create some test runs for different situations. My program schedules students into classes based on priority. Students who submit requests first get priority, and the classes with the least total space (periods times individual period space) have priority for filling up. I've got a test case for students requesting more space than a class has and a test case for testing that the program resorts the inputted classes by the correct priority, which it does. Any other ideas?
« Last Edit: March 16, 2008, 11:56:26 AM by BFM_Edison »
52.87   60.07   46.40   72.73   68.23   55.10   98.27   84.73

Print