• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating Objects -> Generating Table using Collections  RSS feed

 
Gitanajali Magani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey
i m not able to do an application, that maybe simple for many out there, please help me?
i have to generate a table using two collections -> Parent and Child

it should build a table like this
Output - In a table format:
===========================
670Dana Cirovic
AcceptedMark Jones
CompletedSarah Smith
563Eric Suto
670Dana Cirovic
RejectedJudy Garland

i m a beginner with object oriented thing... by doing copy paste things ... i can run examples.

if u could help me then i would be happy

Master are parent records .... with 670 , 563, 670 blah blah ..
the following is the code which i wrote

//////////////////////////////////////// CODE - START///////////////////////////////////////


import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;

public class genTable {
public static void main(String args[]){
/// One Parent with two children
Parent po1 = new Parent();
po1.iCC = 670;
po1.strInjName = "Dana Cirovic";

Collection children = null;

Child co1 = new Child();
co1.strCAStatus = "Accepted";
co1.strAssgTo = "Mark Jones";

children.add(co1);

Child co2 = new Child();
co2.strCAStatus = "Completed";
co2.strAssgTo = "Sarah Smith";

children.add(co2);

po1.childColl = children;

// Second parent with no children
Parent po2 = new Parent();
po2.iCC = 563;
po2.strInjName = "Eric Suto";

po1.childColl = children;

/// Third Parent with one child
Parent po3 = new Parent();
po3.iCC = 670;
po3.strInjName = "Dana Cirovic";

Child co3 = new Child();
co3.strCAStatus = "Rejected";
co3.strAssgTo = "Judy Garland";

Collection children3 = null;
children3.add(co1);
children3.add(co2);

po3.childColl = children3;
HashMap parents = new HashMap();
parents.put("parent1", po1);
parents.put("parent2", po2);
parents.put("parent3", po3);

Iterator it = parents.keySet().iterator();
while (it.hasNext()) {
//For 3 parents
System.out.println("Key: " + it.next());
}
}
}

class Parent
{
int iCC = 0;
String strInjName = null;
Collection childColl = null;
}
class Child
{
String strCAStatus = null;
String strAssgTo = null;
}

//////////////////////////////////////// CODE - END///////////////////////////////////////




i think my code is very clear.. looks like written by a layman... almost like an algorithmn

Here is the table with objects i have used

po1->670Dana Cirovic
co1->AcceptedMark Jones
co2->CompletedSarah Smith
po2->563Eric Suto
po3->670Dana Cirovic
co3->RejectedJudy Garland

Where po's -> are parent elements
and co's -> are child elements
if u have any other solution also .. then u r most welcome..
using java / jsp / struts.. or anything u want to... java side

i kinda created two objects -> parent Class + child class , my code may not be correct.... but my idea is i have one parent obj -> having a collection in it and two vars.
one child obj -> having two vars.
so i have to set values to those vars... dynamically ... n set that child to parent... so that i can display it ... in a loop.

But my code is giving exception called NullPointerException ... So please help me out with a solution.

Thanks a lot,
Gita.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this is what is causing the NPE.
 
Gitanajali Magani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joanne Neal

It is at Line 19:

children.add(co1);

where i m getting NPE

please help me
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
children.add(co1);

where i m getting NPE

please help me[/QB]


Look at the following line:

Note that you do nothing else to instantiate an implementation of the Collection type (e.g, ArrayList, HashSet, etc.).
 
Gitanajali Magani
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys i am getting a NPE @ line number 77 -& gt; u can search for that ... i have mentioned it... while i was retrieving child of second parent -& gt; who has no children.... How should i handle that???




import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.ArrayList;
import java.util.Set;

public class genTable {
public static void main(String args[]) {
String strTable = null;
/// One Parent with two children
Parent po1 = new Parent();
po1.iCC = 670;
po1.strInjName = "Dana Cirovic";

Collection children1 = new ArrayList();

Child co1 = new Child();
co1.strCAStatus = "Accepted";
co1.strAssgTo = "Mark Jones";

Child co2 = new Child();
co2.strCAStatus = "Completed";
co2.strAssgTo = "Sarah Smith";

children1.add(co1);
children1.add(co2);

po1.childColl = children1;

// Second parent with no children
Parent po2 = new Parent();
po2.iCC = 563;
po2.strInjName = "Eric Suto";

po2.childColl = null;

/// Third Parent with one child
Parent po3 = new Parent();
po3.iCC = 670;
po3.strInjName = "Dana Cirovic";

Child co3 = new Child();
co3.strCAStatus = "Rejected";
co3.strAssgTo = "Judy Garland";

Collection child3 = new ArrayList();
child3.add(co3);

po3.childColl = child3;


Map parents = new HashMap();
parents.put("parent1", po1);
//parents.put("parent2", po2);
parents.put("parent3", po3);

Set keys = parents.keySet(); // The set of keys in the map.
Iterator keyIter = keys.iterator();
System.out.println("The map contains the following associations:");

strTable = "& lt;table& gt;\n";
int childCount = 0;
while (keyIter.hasNext()) {
Object key = keyIter.next(); // Get the next key.
Object value = parents.get(key); // Get the value for that key.
//System.out.println( " (" + key + "," + value + ")" );
strTable = strTable + "\n\t & lt;tr id='parent'& gt; \n\t\t & lt;td width='150'& gt; \n\t\t\t & lt;img id='ShowOrHide" + childCount + "' src='plus.gif'& gt; \n\t\t & lt;/td& gt;";
Parent par = (Parent) value;
strTable = strTable + "\n\t\t & lt;td width='150'& gt; " + par.iCC + " & lt;/td& gt;";
System.out.println(par.iCC);
strTable = strTable + "\n\t\t & lt;td width='150'& gt; " + par.strInjName + " & lt;/td& gt;";
strTable = strTable + "\n\t & lt;/tr& gt; ";
System.out.println(par.strInjName);
ArrayList al = (ArrayList) par.childColl;

if (!al.isEmpty()){ //Line Number 77
strTable = strTable + "\n\t & lt;table& gt;";
for (int i=0; i& lt;al.size(); i++){
childCount = childCount + 1;
strTable = strTable + "\n\t & lt;tr id='child" + childCount + "' style='visibility:hidden'& gt; ";
strTable = strTable + "\n\t\t & lt;td width='150'& gt;       & lt;/td& gt; ";
Child ch1 = (Child) al.get(i);
strTable = strTable + "\n\t\t & lt;td width='150'& gt; " + ch1.strCAStatus + "& lt;/td& gt; ";
System.out.println(ch1.strCAStatus);
strTable = strTable + "\n\t\t & lt;td width='150'& gt; " + ch1.strAssgTo + "& lt;/td& gt; ";
System.out.println(ch1.strAssgTo);
strTable = strTable + "\n\t & lt;/tr& gt;";
}
strTable = strTable + "\n\t & lt;/table& gt; ";
}
}
strTable = strTable + "\n & lt;/table& gt; \n";
System.out.println(strTable);
}
}


class Parent {
int iCC = 0;
String strInjName = null;
Collection childColl = null;
}


class Child {
String strCAStatus = null;
String strAssgTo = null;
}


Thank you,
Gita.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is line 77? I can't count that high.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!