• Post Reply Bookmark Topic Watch Topic
  • New Topic

Just quick help with Array or Linked Lists!! Easy Question!  RSS feed

 
Greg Vaslo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I need a very quick tutorial with array lists-I have used the API and searched through 3 books but just can't get it. I have the following code in a program:

public void pickup(Passenger passenger)
{
passengers.add(passenger);
destinations.add(passenger.getDestination());

}

Both destinations and passengers are ArrayLists (or could be linked lists, doesn�t matter)�A passenger object and their destination is added to a "shuttle".

I have a function that I would like to access the destinations list. The function looks like this:

private Location chooseTargetLocation()
{
target = destinations.get(0);
return target;

}

Of course, this doesn�t work, and I don�t know why. Basically I want to pull a location from the destinations and set it equal to target-so basically, how do I pull an object from an Array or linked list and set it equal to another object type variable, so I can get it, set it, all that?

Thanks in advance!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say it doesn't work, what does that mean?

I mean do you get a compile-time error, run-time error, or the program doesn't do what it's supposed to do.
 
Greg Vaslo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that better on the screen name?

It gives me the error: found java.lang.Object but expected Location

Basically, like I said, how does one "Pull" an object from a list and then set it equal to another variable?

Thanks!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Greg Vaslo:
Is that better on the screen name?

It gives me the error: found java.lang.Object but expected Location

Basically, like I said, how does one "Pull" an object from a list and then set it equal to another variable?

Thanks!


It's a compile error. The get() method returns an Object type (unless you use generics). You need to cast it to the object type that you are expecting. Of course, it needs to actually be that type, or you will get a runtime exception.

Thanks for fixing your screen name.

Henry
 
Greg Vaslo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


It's a compile error. The get() method returns an Object type (unless you use generics). You need to cast it to the object type that you are expecting. Of course, it needs to actually be that type, or you will get a runtime exception.

Thanks for fixing your screen name.

Henry


Thanks for getting back to me so quickly on this, and I appreciate your advice on this, and understand what you are saying, but how does one do that? I know how to typecast something, but no idea how to do it in an arrayList.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
destinations.get(0) is returning an Object; you need to cast it to Location before assigning it to a Location variable. It looks like any other cast:

Location target = (Location) destinations.get(0);
 
Greg Vaslo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that worked, but now I got a null pointer exception. -I am so lost, I can't stand this Objects First with BlueJ book-VERY hard for a beginning. Like teaching algebra by showing you calculus.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Greg Vaslo:
Thanks, that worked, but now I got a null pointer exception. -I am so lost, I can't stand this Objects First with BlueJ book-VERY hard for a beginning. Like teaching algebra by showing you calculus.


For small programs, an NPE is very easy to hunt down. The stacktrace should provide the file and line number of where the exception occurred. This should be a good starting point.

And if can't seem to figure it out, then feel free to post again, with the code, (and reference to the line number), and we'll try to point you in the right direction.

Henry
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Greg,

the null pointer exception may result, when the destinations list is empty. Further you should provide your method with an int parameter. As it is now, it will alway get passenger #0.
Wesley Snipes will never turn up...


By checking the passengers list, you could also avoid a null pointer exception:

If the list is empty or the passenger number is out of range, the method returns null (but not a null pointer exception).
If the passenger number fits into the list, a Location object will be returned.


Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi #2,


the code works only, if (as I assumed) target is an instance variable of type Location. If not the line has to be
Location target = (Location)destinations.get(passenger);

target will be method local then, also ok.

Bu.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!