r/javahelp • • Mar 07 '20

New to java, and need some help.

Hey boys, im pretty new to java and im just trying to fix this no-arg constructor compile error.

-----------------------------------------------------------

src\com\rs2hd\model\NPCDrop.java:13: error: missing method body, or declare abstract

public NPCDrop()

----------------------------------------------------------

Here is the code.

----------------------------------------------------------

package com.rs2hd.model;

import com.rs2hd.util.XStreamUtil;

import com.rs2hd.util.log.Logger;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class NPCDrop {

public NPCDrop();

public static Map<Integer, NPCDrop> npcDrops = new HashMap<Integer, NPCDrop>();

@>SuppressWarnings("unchecked")

public static void load() throws FileNotFoundException {

List<NPCDrop> list = (List<NPCDrop>) XStreamUtil.getXStream().fromXML(new FileInputStream("data/npcDrops.xml"));

for (NPCDrop drop : list) {

npcDrops.put(drop.getNpc(), drop);

}

Logger.getInstance().info("Loaded " + npcDrops.size() + " npc drops.");

}

public static NPCDrop forId(int id) {

return npcDrops.get(id);

}

public class Drop {

private int chance;

private Item[] items;

public int getChance() {

return chance;

}

public Item[] getItems() {

return items;

}

}

private int npc;

private Drop[] drops;

public int getNpc() {

return npc;

}

public Drop[] getDrops() {

return drops;

}

}

--------------------------------------------------------------------

If you can provide an insight that would be fantastic, thank you.

1 Upvotes

8 comments sorted by

2

u/doodooz7 Mar 07 '20

That’s invalid syntax. Get rid of it. Is it supposed to be a constructor?

1

u/[deleted] Mar 07 '20

yes it is, when i ran my compiler it gave me an xstream error and said i needed a No Arg constructor, and this is my first time using it so im 100% sure im doing this like a brainlet.

2

u/[deleted] Mar 07 '20

and are you saying the

" public NPCDrop(); " is invalid syntax?

2

u/doodooz7 Mar 07 '20

Yes that’s invalid, turn it into a constructor with no arguments.

https://www.w3schools.com/java/java_constructors.asp

1

u/[deleted] Mar 07 '20

com.thoughtworks.xstream.converters.ConversionException: Cannot construct com.rs2hd.model.NPCDrop$Drop as it does not have a no-args constructor : Cannot construct com.rs2hd.model.NPCDrop$Drop as it does not have a no-args constructor

This is the error my program is giving me now, even though ive already added the no args constuctor, im starting to think its an issue with my jdk/jre versions.

1

u/reaper-is-happy Mar 07 '20

Try and reinstall it then

2

u/radulfr2 Mar 07 '20

Like the error message says, the constructor is missing a body (ie. curly braces { } and possible code inside it). If you don't need to have any code in the constructor, you can just leave the constructor out altogether and Java will create a default, empty constructor for you.

2

u/[deleted] Mar 07 '20

bro you’re a saint. i’m coming from a long hiatus from c# since i’ve been on deployment, and i’ve gotta get back to thinking like a programmer and not a big dummy. much love.