473,324 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.add("Monday");
DaysOfTheweek.add("Tuesday");
DaysOfTheweek.add("Wednesday");
DaysOfTheweek.add("Thursday");
DaysOfTheweek.add("friday");
DaysOfTheweek.add("Saturday");
DaysOfTheweek.add("Sunday");
}

public void printDays() {

int i = 0;
while (i < DaysOfTheweek.size()-1) {
System.out.println(""+DaysOfTheweek.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 !!!!
May 29 '07 #1
5 1269
prometheuzz
197 Expert 100+
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.
May 29 '07 #2
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
May 29 '07 #3
prometheuzz
197 Expert 100+
Thanks for super quick reply !!

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


......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:
Expand|Select|Wrap|Line Numbers
  1. ArrayList list = new ArrayList();
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:
Expand|Select|Wrap|Line Numbers
  1. String anElement = (String)list.get(4);
But now, since Java 1.5, you can write it like this:
Expand|Select|Wrap|Line Numbers
  1. ArrayList<String> list = new ArrayList<String>(); // we can only store Strings in it
  2. // ...
  3. String anElement = list.get(4); // no need to cast since the compiler "knows" it is a String
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.
May 29 '07 #4
Thanks again got an exam tomorow so this has been awsome info life saver!!!
May 29 '07 #5
r035198x
13,262 8TB
Thanks again got an exam tomorow so this has been awsome info life saver!!!
Well, good luck with your exam StupidStudent.
May 29 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: godsella | last post by:
First i have two stored procedures, i have passed the values of each one into two different arraylists of dates. how can i compare the two arraylists of dates? Thanks in advance
5
by: drdave | last post by:
I would like to have ten arraylists created within a loop.. is there a conversion or something I can do to acheive this.. pseudo: Dim counter As Integer = 0 Dim ArrName As ArrayList ...
0
by: steve | last post by:
I'm looking for a code example how to compare the values in a given record in different arraylists two arraylists, two fields in each record, both defined as string I'm thinking that it's...
4
by: Andy in S. Jersey | last post by:
I would like to create an unknown number of Arraylists, meaning, I don't know how many I should create until runtime. I will be reading a table, and 0,1,2, or more fields have to be put into...
1
by: Newbie19 | last post by:
I'm just learning java arrays/arraylists and was wondering what are the best books for learning java arrays/arraylists? I know practice is the best way to learn, but I have a hard time...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.