I need to create a list which contains objects of a particular class. I am having problem in retrieving the objects back from the list.
Here is my code for insertion into list-

import java.util.LinkedList;
import java.util.ListIterator;

public class listchek {

    int i;
    String name;

    public listchek(int k, String n)
    {
        i=k;
        name=n;
    }
        static LinkedList<listchek> list;

    public static void add(listchek lc)
    {
        list.add(lc);
    }

    public static void getlistelements()
    {


    }

}

 class insert
{
    public static void main(String arg[])
    {
        listchek ob1=new listchek(1, "first");
        listchek ob2=new listchek(2, "sec");
        listchek ob3=new listchek(3, "third");
        listchek ob4=new listchek(4, "fourth");

        listchek.add(ob1);
        listchek.add(ob2);
        listchek.add(ob3);
        listchek.add(ob4);


    }
}

How do i get the object values back from list?

Look @ the get method inside linked list

Look @ the get method inside linked list

Tried that.

List has elements of type listchek (class). I want to store it in a variable V.
If I am not wrong i think I can access the further class variables by V.i and V.name.

What should be the type of V? I tried creating - listchek V; but its giving an error.

N I was trying to use the listIterator method also so that I can get all the elements one by one.

Comment on your variable name: V
Coding conventions for variables say they start with a lowercase letter.
Single letter names are not selfdocumenting as far as what the variable is used for. Use a name what when read shows what the variable contains or what it is used for.
The exceptions are for loop control variables: i,j,k

What should be the type of V? I tried creating - listchek V; but its giving an error.

When you get an error, please copy and paste the full text here.

Edit: What Norm said

Comment on your variable name: V
Coding conventions for variables say they start with a lowercase letter.
Single letter names are not selfdocumenting as far as what the variable is used for. Use a name what when read shows what the variable contains or what it is used for.
The exceptions are for loop control variables: i,j,k

Oh oh I was just giving an example that suppose if I use a variable V to get the list objects!!!
I just wanted to know its tyoe that I should use...

When you get an error, please copy and paste the full text here.

Ok here's my function-

public static void getlistelements()
	{
		listchek element;
		
		element=list.listIterator(0);  //Error : Type mismatch: cannot convert from ListIterator<listchek> to listchek
		
	}

What I want to do is I want to access the 4 objects which I have entered added in the list.

Type mismatch: cannot convert from ListIterator<listchek> to listchek

The error message says it all. The variable element is of type: listchek.
The listIterator does NOT return an object of that type. You need to read the API doc for the listIterator method and see what type it returns.

The error message says it all. The variable element is of type: listchek.
The listIterator does NOT return an object of that type. You need to read the API doc for the listIterator method and see what type it returns.

Okk I managed to correct this a bit.

mport java.util.LinkedList;
import java.util.ListIterator;

public class listchek {
	
	int i;
	String name;
	
	public listchek(int k, String n)
	{
		i=k;
		name=n;
	}
		static LinkedList<listchek> list;
	
	public static void add(listchek lc)
	{
		list.add(lc);
	}

	public static void printlist()
	{
		ListIterator<listchek> element;
		
		element=list.listIterator(0);  
		
	}
	

}

public class Insert {

	public static void main(String arg[])
	{
		listchek ob1=new listchek(1, "first");
		listchek ob2=new listchek(2, "sec");
		listchek ob3=new listchek(3, "third");
		listchek ob4=new listchek(4, "fourth");
		
		listchek.add(ob1);
		listchek.add(ob2);
		listchek.add(ob3);
		listchek.add(ob4);
		
			
	}
}

When I run this code I get error in line 18 and 41 above. -
Exception in thread "main" java.lang.NullPointerException
at Test.listchek.add(listchek.java:21)
at Test.Insert.main(Insert.java:12)

M not getting the reason behind it :(

The reason for a NPE is because you have not given a value to a variable and the variable is null.
To prevent getting a NPE, either assign a value to the variable
or test if the variable is null before trying to use it.

Where do you give "list" a value? You need something like:
list = A_VALID_VALUE_FOR_LIST;

The reason for a NPE is because you have not given a value to a variable and the variable is null.
To prevent getting a NPE, either assign a value to the variable
or test if the variable is null before trying to use it.

Where do you give "list" a value? You need something like:
list = A_VALID_VALUE_FOR_LIST;

The list which I have created should store objects of listchek.

I have created different objects of listchek and given them values. The I am passing it to list to be added.
I think the values are being passed to the object's variables int and String. How do I give values to the object then to store them in the list?

How do I give values to the object

Use the class's constructor to give values to the object.
Or use settor methods to give values to a class object to save

store them in the list?

Use a method to add the objects to the list

Use the class's constructor to give values to the object.
Or use settor methods to give values to a class object to save

Use a method to add the objects to the list

Tats what I have done!! i am passing the values to the object through listchek constructor only..

N I have defined a separate method to add it to the list!

Please P the C showing what U have D.

Please P the C showing what U have D.

I have already posted the code..
N I have solved the problem myself. The was a problem in the linkedlist declaration.

Neways thnx:)

G L

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.