Jump to content





Recent Status Updates

  • Photo
      16 Apr
    Kadence

    If you're reading this, you're on my profile and I know you're on my profile because I'm probably viewing yours.

    Show comments (6)
  • Photo
      10 Apr
    Poe

    Finally (and hopefully) i'm getting a team together that knows a little of this and a little of that; and maybe all my open source projects that are half written can begin to be released. :)

View All Updates
Photo
- - - - -

Not able to create new ItemSet


  • Please log in to reply
17 replies to this topic

#1 Nyfer

Nyfer

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 30 August 2012 - 07:24 PM

hey this my item class

public class Item {

	 private String id;
private int count;
private String name;


public int getcount()
{
	 return this.count;
}



	 public void setcount(int x)
{
	 this.count=x;
}

	
public Item(String name)
	 {
		 this.name=name;
		 this.id = "";
	
	 }

public Item(String id, String name)
	 {
		 this.name=name;
	 this.id=id;
	
	 }

public String getItemName()
{
	 return this.name;
}

public String getItemId()
{
	 return this.id;
}

}



This is my itemSet class which will contain List of items

public class ItemSet extends ArrayList<Item> {

private List<Item> hold;



ItemSet(Item item)
{
	 this.hold.add(item);
}

ItemSet() {
	
}


public List<Item> getItemSet()
{
	 return this.hold;
}

}


this is my transaction class

import java.sql.ResultSet;
import java.util.ArrayList;


public class Transaction extends ArrayList<ItemSet> {

// ArrayList<String> l;
public ItemSet getUniqueItem()
{
	 ResultSet rs;
	 Database d=new Database();
	 ItemSet unique=new ItemSet();
	 unique.clear();
	 String query="Select id,name from item";
	 rs=d.sendQuery(query);
		 try{
		 while(rs.next())
			 {
			 //System.out.println(rs.getString(1)+"\t"+rs.getString(2));
			 Item item=new Item(rs.getString(1),rs.getString(2));
			 unique.add(item);	
	 }
	 }catch(Exception e){System.out.print(e.getMessage());
		 }

	 for(Item item:unique)
	 {
		 // System.out.println(item.getItemId()+": "+item.getItemName());
	 }
		
		
	
	 return unique;
}



public int countItems(ItemSet itemset)
{
	 ResultSet rs;
	 Database d=new Database();
	 String query="";
	 int count=0;
	 for(Item i:itemset)
	 {
		 String id=i.getItemId();
		 query="SELECT count(*) FROM `item_transaction` where item_id="+i;
			 rs=d.sendQuery(query);
			 try{
			 while(rs.next())
			 {
				 //System.out.print(rs.getString(1));
				 count=Integer.parseInt(rs.getString(1));
				 System.out.print(count+"\t");
			 }
			
			 }catch(Exception e){}
	 }
	
		 return count;
}


/*public List countItems(List<String> item)
{
	 ResultSet rs;
	 Database d=new Database();
	 String query="";
	 List<String> item_count=new ArrayList<String>();
	
	 for(String t:item)
	 {
			 query="SELECT count(*) FROM `item_transaction` where item_id="+t;
			 rs=d.sendQuery(query);
			 try{
			 while(rs.next())
			 {
				 item_count.add(rs.getString(1));
			 }
			 }catch(Exception e){}
	 }
	
		 return item_count;
	
}*/



}





This is my main class
public class Ap {

public static void main(String args[])
{
	 Transaction t=new Transaction();
	 Transaction Ci=new Transaction();
	 Transaction Li=new Transaction();

	 ItemSet I=t.getUniqueItem();
	 for(Item i:I)
	 {
		 try{
		 ItemSet iS=new ItemSet(i); // am not able to create new ItemSet getting null //exception
		 }catch(Exception e){System.out.print(e.getMessage());
		 }
		
		 }

	
	 Item it=null;

	 for(ItemSet itemSet:Ci)
	 {
		 int x=t.countItems(itemSet);
	 // System.out.print(x+"\t");
	 it.setcount(x);
	 if(it.getcount()>=2)
	 {
		 //Li.add(itemset);
	 }
	 }
	
	 Iterator iter=Li.iterator();
	 while(iter.hasNext())
	 {
		 // System.out.print(iter.next()+"\t");
	 }
	
	
}

}


Am not able to create new ItemSet . getting null exception .Please help
  • 0

#2 BlackRabbit

BlackRabbit

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2,939 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 31 August 2012 - 04:14 AM

a question:

why does an item extends : ArrayList<String> ?

i really don't understand quite well what you are trying to do, or why did you choose this path of action,
i am not sure you need single items being and array
  • 0

#3 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 388 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 31 August 2012 - 07:57 AM

You have an empty catch block that could be ignoring an error. Add a call to the printStackTrace() method to all catch blocks so that the code does not ignore any errors.

Try debugging the code by adding println statements to show the values of variables when they change and to show execution flow. For example, print out a message every time something is added to unique.
  • 0

#4 Nyfer

Nyfer

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 31 August 2012 - 09:00 AM

@
BlackRabbit yea your correct. My mistake. Please have a look at my edited code now
  • 0

#5 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 388 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 31 August 2012 - 09:02 AM

Does the code work now? If not, did you try debugging the code by adding some println statements to see what it was doing? The call to printStackTrace() is still missing in the posted code.
  • 0

#6 Nyfer

Nyfer

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 31 August 2012 - 09:40 AM

see am creating a new ItemSet and adding item i to it

ItemSet iS=new ItemSet(i);

But am getting nullPointer exception
Please look at my ItemSet class and tell me where am going wrong
  • 0

#7 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 388 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 31 August 2012 - 09:54 AM

am getting nullPointer exception

Please post the full text of the error message so we can see where the problem is.
Do you know what variable has the null value? If not, add a println just before where the NPE happens that prints out the values of all the variables used on the line where the NPE happens.
  • 0

#8 Nyfer

Nyfer

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 31 August 2012 - 09:57 AM

Exception in thread "main" java.lang.NullPointerException
at ItemSet.<init>(ItemSet.java:25)
at Ap.main(Ap.java:29)
Java Result: 1

line 25 in ItemSet class is
this.hold.add(item);

line 29 in Ap class is
ItemSet itemSet=new ItemSet(i);

  • 0

#9 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 388 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 31 August 2012 - 10:02 AM

Where do you give a value to the hold variable? The code defines it but does not assign it a value.

If Item IS an ArrayList, why define another list? Why not just add to Item object directly?
  • 0

#10 Nyfer

Nyfer

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 31 August 2012 - 10:03 AM

ok so please help me in this. am stuck at that point
  • 0

#11 Norm

Norm

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 388 posts
  • Location:Eastern Florida
  • Programming Language:Java, C++, Assembly

Posted 31 August 2012 - 10:12 AM

Why does ItemSet extend ArrayList? See post#2.

I don't understand what the ItemSet class is doing with a list it creates each time a new instance of ItemSet is created.
Will that list ever have more than one value in it? Why not just save the value in a class variable in the ItemSet object?
  • 0

#12 Nyfer

Nyfer

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 31 August 2012 - 10:15 AM

yes ItemSet will have List of items e.g

{I1, I2,I3 } -> one itemset

{I1,I3,I4} ->another itemset
  • 0