Search code examples
javaobjectincompatibletypeerror

java.lang.object cannot be converted to (selfmade class)


I'm new to java, just about to finish programming fundamentals at uni, so I'd appreciate thorough explanation of any answers. Thanks. Also by the way I am using BlueJ as part of my university training so that might be relevant.

Anyway, here's the problem; I made a text adventure for one of my assignments, and it was very procedural designed, so I decided to revamp it afterwards into a more object oriented program. I think all you need to know about are 4 class' (some unimportant bits are left out otherwise this would be pages long but if you think they are important let me know);

inventory

static room room;


public inventory(room room)
{
    this.room = room;
}

public static room getRoom()
{
    return room;
}

room

ArrayList <object> objects = new ArrayList <object>();
public ArrayList getobjects()
{
    return objects;
}

object

ArrayList<String> names = new ArrayList<String>();
public ArrayList getNames()
{
    return names;
}

textparse (textparse has a textparse method)

public static void textparse(String line)
{
    if (line.indexOf(" ") != -1){
        int space = line.indexOf(" ");
        noun = line.substring(space + 1, line.length()).toLowerCase();
        verb = line.substring(0, space).toLowerCase();
    }
    else
        verb = line;
    verbparse();
}

Here's the problem; room has an arraylist of objects which are present in it, object has an arraylist of names they can be called by the player. This is my code to attempt to check whether the noun recognized earlier by the textparse class matches any of the objects' names. This is in the textparse class by the way;

public static object nounparse()
{
    for (int counter = 0; counter < inventory.getRoom().getobjects().size(); counter++)
    {
        object current = inventory.getRoom().getobjects().get(counter);
        if (current.getNames().contains(noun)){
            return current;
        }
    }
    return null;
}

It returns the error 'incompatible types:java.lang.Object cannot be converted to object' referencing this line

object current = inventory.getRoom().getobjects().get(counter);

I'm not sure why it thinks that the output of this is an Object, but due to my limited experience in the field I'm not even sure what an Object is besides a general classifier of a instance of a class. Any help would be appreciated. Thanks!


Solution

  • You are missing a generic parameter. Your getRooms() should return ArrayList<object>, if that's your desired behavior.

    Also, you should really rename your object into something else, like Thing and Item, or it may cause confusion, typo, wrong reads (i.e. you may later think you wrote Object not object), etc. All class names are also recommended to be written in Big Camel Case.

    Further readings

    Java Generics: Java Generics Tutorial at Oracle.com

    Java naming convention: (too lazy to find one, see the answer of @DanielWiddis)