Please help with arrayLists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • StupidStudent
    New Member
    • May 2007
    • 3

    Please help with arrayLists

    Hi everyone

    I am fairly new to java and ony know the reall basics (if statements, while loops) that kind of stuff however I have started to dabble in the crazy world of lists and collections.... I am using BlueJ (the latest version) and before you say it.......... I know everyone has told me this is not to good, however it is what the uni force use to use...;)
    Any way i am writing this code just to add daysOfTheweek to an arraylist and then use another method to print to terminal;

    import java.util.*;

    public class Collections
    {
    ArrayList DaysOfTheweek;

    public Collections() {
    ArrayList DaysOfTheweek = new ArrayList();
    }

    public void addDays() {

    DaysOfTheweek.a dd("Monday");
    DaysOfTheweek.a dd("Tuesday");
    DaysOfTheweek.a dd("Wednesday") ;
    DaysOfTheweek.a dd("Thursday") ;
    DaysOfTheweek.a dd("friday");
    DaysOfTheweek.a dd("Saturday") ;
    DaysOfTheweek.a dd("Sunday");
    }

    public void printDays() {

    int i = 0;
    while (i < DaysOfTheweek.s ize()-1) {
    System.out.prin tln(""+DaysOfTh eweek.get(i));
    i++;
    }
    }
    }

    The problem I keep getting is a null pointer execption?? I know this means it is trying to access an empty part of the list. I just cant see where I have gone wrong so PLEASE if anyone can help me out of this it would be much appreciated :)

    THANKS !!!!
  • prometheuzz
    Recognized Expert New Member
    • Apr 2007
    • 197

    #2
    Couple of tips:

    - don't name you class Collections because this is also a class in the java.util package. A better name would be to call it MyCollections or CollectionsDemo;

    - use Java's code conventions. The most impotant "rules" are: classes (and interfaces) start with a capital and variables start with a lowercase. So DaysOfTheweek should be better named daysOfTheweek.

    Now your problem. Your ArrayList is null because you did not initiate it. You did initiate a local variable called DaysOfTheweek inside your constructor. This variable is only "known" inside that constructor because you added ArrayList in front of it.

    Good luck.

    Comment

    • StupidStudent
      New Member
      • May 2007
      • 3

      #3
      Thanks for super quick reply !!

      Thanks for rules have changed code accordingly.... .. AWSOME advice works like a dream thanks !!!!!

      ......just quickly if anyone knows? when i run this code i get a compiler warning stating to recompile -Xlint:unchecked for details.... does anyone know what this means???

      Thanks again for all your help

      Comment

      • prometheuzz
        Recognized Expert New Member
        • Apr 2007
        • 197

        #4
        Originally posted by StupidStudent
        Thanks for super quick reply !!

        Thanks for rules have changed code accordingly.... .. AWSOME advice works like a dream thanks !!!!!
        You're welcome.


        Originally posted by StupidStudent
        ......just quickly if anyone knows? when i run this code i get a compiler warning stating to recompile -Xlint:unchecked for details.... does anyone know what this means???

        Thanks again for all your help
        That's because you're using Java version 1.5 (or higher) and are not using generics.
        Pre-Java 1.5 when writing:
        [CODE=java]ArrayList list = new ArrayList();[/CODE]
        you could add any Object (Integer's, String's, ...) in that list. And when accessing an element, you had to cast it back like this:
        [CODE=java]String anElement = (String)list.ge t(4);[/CODE]

        But now, since Java 1.5, you can write it like this:
        [CODE=java]ArrayList<Strin g> list = new ArrayList<Strin g>(); // we can only store Strings in it
        // ...
        String anElement = list.get(4); // no need to cast since the compiler "knows" it is a String[/CODE]

        To get back to your compiler warning: if you use Java 1.5 (or higher) and NOT use generics (the <...> brackets), the compiler gives you a warning about it.

        Good luck.

        Comment

        • StupidStudent
          New Member
          • May 2007
          • 3

          #5
          Thanks again got an exam tomorow so this has been awsome info life saver!!!

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by StupidStudent
            Thanks again got an exam tomorow so this has been awsome info life saver!!!
            Well, good luck with your exam StupidStudent.

            Comment

            Working...