This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Passing handle to ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing handle to ArrayList" Watch "Passing handle to ArrayList" New topic
Author

Passing handle to ArrayList

Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
Hi!
There is private variable Customers cust in Model class, Customers extends ArrayList.
Values of cust passed to cu and populated in Model in populate() method:
Model{
private Customers cust = new Customers();
Customers getCustomers(){ return cust;}
//i expect to use this method from other class to get cust
static void populate(Model model){
Customers cu = model.getCustomers();
cu.add(eq);}
}
Now, in View class representing AWT visual version of Model class I want to assign value of populated cust to custView.
If I trying something like:
View{
Model mo = new Model();
Customers custView = mo.getCustomers();
}
and later i am trying to iterate it:
public void itemStateChanged(ItemEvent e)
{
Iterator iter = null;
if (e.getStateChange() == ItemEvent.SELECTED)
{
list.removeAll();
if(e.getItem().equals("Customers List"))
{
iter = custView.iterator();
while(iter.hasNext())
Borrower a = (Borrower)iter.next();list.add(a.getListString());
}
}
}
}
Everything is compiling well, i got a "picture",
but when i selecting an item i got runtime exeption:
Exception occurred during event dispatching:
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:490)
at java.util.ArrayList.get(ArrayList.java:304)
at View.itemStateChanged(View.java:115)
at java.awt.Choice.processItemEvent(Choice.java:486)
at java.awt.Choice.processEvent(Choice.java:458)
at java.awt.Component.dispatchEventImpl(Component.java:2399)
at java.awt.Component.dispatchEvent(Component.java:2312)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:301)
at java.awt.EventDispatchThread.pumpOneEventForComponent(EventDispatchThread.java, Compiled Code)
it looks like my custView is null - or may be iterator written wrong?
Any advise will be highly appreciate!
Vladimir
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Vladimir,
I didn't see a default constructor for Model in your example. Without seeing the constructor, I am guessing, but it looks like you are creating a fresh Model object and calling getCustomers() without populating the array. Could this be happening?
Nick
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
Hi Nick!
I posted View becouse i suspect problem with syntax.
It is under constraction , but it is running.
Model itself running without problems.
Model{
private Customers cust = new Customers();
Customers getCustomers(){ return cust;}

//Borrowers - objects that ArrayList populated with.
static void populate(Model model){
Customers cu = model.getCustomers();
static void populate(Model model)
{
Customers cust = model.getCustomers();

String [][] borrData ={ {...},{..}.......}
Borrower eq = new Borrower();
eq.setId(borrData[i][1]);
eq.setName(borrData[i][2]);
eq.setEmail(borrData[i][3]);
cust.add(eq);
}
}
Runtime exception for this version posted.
Exeption occured whith mouse clicking on list "customers"
Thanks a lot
import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;
public class View extends Frame implements ItemListener
{
private Model model ;
private String myName = "Organizer ";
TextArea lst;
List list;
Customers custView ;

public View(Model in_model)
{super();
setTitle("Equipment Manager by " + myName);
model = in_model;
}
public Customers makeModel()
{
model = new Model();
custView = (Customers)model.getCustomers().clone();
return custView;
}
public void Visual()
{
Label collections = new Label("Choose collection");
Choice c = new Choice();
c.addItemListener( this );
Label itemss = new Label("Choose Item :");
list = new List(5);

Button report = new Button("Report");
Panel reportEH = new Panel();
reportEH.add(report);
Button field = new Button("Field");
Panel fieldEH = new Panel();
reportEH.add(field);
lst = new TextArea(10, 20);
TextArea f = new TextArea(10, 20);
setLayout(new BorderLayout());
setSize(600, 200);
Panel panel1 = new Panel();
panel1.setLayout( new GridLayout(3, 3, 0, 30));
Panel panel2 = new Panel();
Panel panel3 = new Panel();
c.addItem("<NONE> ");
c.addItem("Inventory List");
c.addItem("Customers List");
c.addItem("Loan History List");
panel1.add(collections);
panel1.add(c);
panel1.add(itemss);
panel1.add(list);
panel1.add(fieldEH);
panel1.add(reportEH);
panel2.add(lst);
panel3.add(f);
add(panel1, BorderLayout.NORTH);
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.WEST);
pack();

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
show();
}
public void itemStateChanged(ItemEvent e)
{
Iterator iter = null;
if (e.getStateChange() == ItemEvent.SELECTED)
{
list.removeAll();
if(e.getItem().equals("Customers List"))
{
iter = custView.iterator();// custView == null ???
while(iter.hasNext())
{
Borrower a = (Borrower)iter.next();list.add( a.getListString());
}
}
}
}
}
Exception occurred during event dispatching:
java.lang.NullPointerException
at View.itemStateChanged(View.java, Compiled Code)
at java.awt.Choice.processItemEvent(Choice.java:486)
at java.awt.Choice.processEvent(Choice.java:458)
at java.awt.Component.dispatchEventImpl(Component.java:2399)
at java.awt.Component.dispatchEvent(Component.java:2312)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:301)
at java.awt.EventDispatchThread.pumpOneEventForComponent(EventDispatchTh
read.java, Compiled Code)
at java.awt.EventDispatchThread.pumpEventsForComponent(EventDispatchThre
ad.java:95)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:90)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Passing handle to ArrayList
 
Similar Threads
MVC -2+ views for 1 controller??
Struts and Hibernate how to display in jsp file
custom view
manager class with testing - any help please!!!
Error in run method and class - help!