For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2007 > ArrayList help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ArrayList help
TonyB

2007-02-23, 7:15 pm

I'm trying out some examples of ArrayList, but keep getting
nullpointerexception errors.
Bascially I'm creating a new ArrayList object in a class constructor.
code fragment is
public class TestList
{
private ArrayList rlist ;
// Constructor for objects of class RouteList
public TestList() {
ArrayList rlist = new ArrayList() ;
}
public boolean addItem(int type){
if (rlist.size() == 0 ) //(or if(rlist.isEmpty() == true)
.......}
}
In one of the methods to the class I add an item to the list, if the item is
not already in the list. In the method I first test to see if the list is
empty and this test causes the exception. I tried rlist.isEmpty(). Also
tried rlist.size() both have the same error.
It appears that using either of the methods cause an exception. Is there any
safe way of checking if the ArrayList is empty ?
Tony


TonyB

2007-02-23, 7:15 pm

"TonyB" <tonyb@kerrisway.freeserve.co.uk> wrote in message
news:lhLDh.283781$_92.262880@fe05.news.easynews.com...
> I'm trying out some examples of ArrayList, but keep getting
> nullpointerexception errors.
> Bascially I'm creating a new ArrayList object in a class constructor.
> code fragment is
> public class TestList
> {
> private ArrayList rlist ;
> // Constructor for class TestList ********
> public TestList() {
> ArrayList rlist = new ArrayList() ;
> }
> public boolean addItem(int type){
> if (rlist.size() == 0 ) //(or if(rlist.isEmpty() == true)
> ......}
> }
> In one of the methods to the class I add an item to the list, if the item
> is not already in the list. In the method I first test to see if the list
> is empty and this test causes the exception. I tried rlist.isEmpty(). Also
> tried rlist.size() both have the same error.
> It appears that using either of the methods cause an exception. Is there
> any safe way of checking if the ArrayList is empty ?
> Tony

Code fragment copied wrong, and corrected above. Actual code compiling fine.
Regards Tony


Knute Johnson

2007-02-23, 7:15 pm

TonyB wrote:
> I'm trying out some examples of ArrayList, but keep getting
> nullpointerexception errors.
> Bascially I'm creating a new ArrayList object in a class constructor.
> code fragment is
> public class TestList
> {
> private ArrayList rlist ;


You create a reference to an ArrayList here. It's default value is null.

> // Constructor for objects of class RouteList
> public TestList() {
> ArrayList rlist = new ArrayList() ;


You create a reference and an ArrayList here but it's scope is only to
the next line.

> }
> public boolean addItem(int type){
> if (rlist.size() == 0 ) //(or if(rlist.isEmpty() == true)


This line is accessing the instance variable at the top and it is null.

> ......}
> }
> In one of the methods to the class I add an item to the list, if the item is
> not already in the list. In the method I first test to see if the list is
> empty and this test causes the exception. I tried rlist.isEmpty(). Also
> tried rlist.size() both have the same error.
> It appears that using either of the methods cause an exception. Is there any
> safe way of checking if the ArrayList is empty ?
> Tony
>
>


class someClass {
ArrayList al;

public someClass() {
al = new ArrayList();

Now this ArrayList is visible in your whole class and in the package for
that matter.

--

Knute Johnson
email s/nospam/knute/
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com