• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Passing handle to ArrayList

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Run away! Run away! Here, take this tiny ad with you:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!