0

My code is meant to take in a bunch of variables from a GUI and have them inserted into a linked list of nodes to be called when all the information is to be printed. My declaration looks like this (Just incase this information is needed).

public abstract class item{
    public int ID;
    public String name;
     //using 2 variables for this example
        
    public item(String name, int ID) {
        this.name = name;
        this.ID = ID;}
        
    public String getName() {
        return name;}

    public void setName(String name) {
        this.name = name;}
        
    public int getID() {
        return ID;}
        
    public void setID(int ID) {
        this.ID = ID;}
}

The problem, however, is when I try to insert the information into the first node, and any other node, using this method

    public static item first(String info) {
        item stuff = new item(); //"Cannot instantiate the type (class).item"
        String array[] = info.split(", "); 
        //"ID, name" becomes "ID" "name"
        
        stuff.ID = Integer.parseInt(array[0]);
        stuff.name = array[1];
        
        return stuff;
    }

It gives me that specific error for trying to make a node to insert into my linked list. My code won't accept setting stuff equal to anything else other than null which gives me a null pointer exception for the first variable. Is there anything I can change to fix this?

  • 2
    your `class item` should be `class Item`. Classes should have `PascalCase` names. – Dai Sep 08 '21 at 21:40
  • Ok so the variable name is case sensitive. I can see that. I’ll try it when I get back from work – mario segale Sep 09 '21 at 15:58
  • OK so it looks better but what would the declaration look like if I want to make it like a linked list where I use ```public Item next``` and/or ```public Item prev```. it wants something like ```Item stuff = new Item(24, john, stuff, stuff);``` except stuff doesn't work for both cause it isn't initialized. – mario segale Sep 10 '21 at 17:21
  • Ok so I tried to see if I can do without the Next and Prev items but now Its saying that the variable isn't initialized and no enclosing instance of type (class) is accessible. what would this mean and how can I fix that. – mario segale Sep 10 '21 at 19:35

1 Answers1

1

Use your Item's constructor:

    public static item first(String info) {

        String array[] = info.split(", ");
        
        int    id   = Integer.parseInt(array[0]);
        string name = array[1];

        return new Item( id, name );
    }
Dai
  • 141,631
  • 28
  • 261
  • 374
  • Ok I did that and I also made the Item node static and It's not giving me any errors so far. I'll come back to see if I have any more issues and if I don't, the whole thing works. Thanks for the help. – mario segale Sep 11 '21 at 16:48
  • Ok now I'm having a similar issue with a string array. ```String trans[] = null;```. it won't accept anything other than null which gives me another null pointer exception. – mario segale Sep 12 '21 at 00:38