Author Topic: NullPointerException bei eigenem Plugin  (Read 910 times)

Rising Darkness

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Mightful Noobs
NullPointerException bei eigenem Plugin
« on: April 06, 2019, 11:09:40 PM »
Hallo,

ich bekomme in meinem Sourcecode durch den Bot einen NullPointerException.
Ich weiß nicht mehr weiter, darum dachte ich, ich frag mal hier:
Fehler:
Code: [Select]
java.lang.NullPointerException
at java.util.concurrent.ConcurrentHashMap.putVal(ConcurrentHashMap.java:1011)
at java.util.concurrent.ConcurrentHashMap.putIfAbsent(ConcurrentHashMap.java:1535)
at java.lang.ClassLoader.getClassLoadingLock(ClassLoader.java:463)
at java.lang.ClassLoader.loadClass(ClassLoader.java:404)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at de.stefan1200.jts3servermod.JTS3ServerMod.d(Unknown Source)
at de.stefan1200.jts3servermod.JTS3ServerMod.b(Unknown Source)
at de.stefan1200.jts3servermod.JTS3ServerMod.b(Unknown Source)
at de.stefan1200.jts3servermod.h.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)

Das geschriebene Plugin:
Code: [Select]
package de.mightful_noobs.Voting;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Vector;

import de.stefan1200.jts3servermod.BotConfigurationException;
import de.stefan1200.jts3servermod.FunctionExceptionLog;
import de.stefan1200.jts3servermod.interfaces.HandleBotEvents;
import de.stefan1200.jts3servermod.interfaces.HandleClientList;
import de.stefan1200.jts3servermod.interfaces.JTS3ServerMod_Interface;
import de.stefan1200.jts3servermod.interfaces.LoadConfiguration;
import de.stefan1200.jts3serverquery.JTS3ServerQuery;
import de.stefan1200.jts3serverquery.TS3ServerQueryException;
import de.stefan1200.util.ArrangedPropertiesWriter;

public class Voting implements HandleBotEvents, LoadConfiguration, HandleClientList {

public static void main(String[] args) {
}

private JTS3ServerMod_Interface modClass = null;
private JTS3ServerQuery queryLib = null;
private String configPrefix = "";
private boolean pluginEnabled = false;

private Vector<Integer> clientList = new Vector<Integer>();
private FunctionExceptionLog fel = new FunctionExceptionLog();
private String sgid = "";
private String key = "";
private String message = "";
private boolean firstInit = true;

@Override
public void initClass(JTS3ServerMod_Interface modClass, JTS3ServerQuery queryLib, String prefix) {
this.modClass = modClass;
this.queryLib = queryLib;
configPrefix = prefix.trim();
}

public void initConfig(ArrangedPropertiesWriter config) {
config.addKey(configPrefix + "_sgid", "Servergroup-ID?");
config.addKey(configPrefix + "_key", "API-Key");
config.addKey(configPrefix + "_message", "Voting Message");
}

@Override
public void handleOnBotConnect() {
if (!pluginEnabled) {
return;
}
String msg = "Voting is enabled. Servergroup: " + sgid.toString() + " API-Key: " + key.toString();
modClass.addLogEntry(configPrefix, JTS3ServerMod_Interface.ERROR_LEVEL_INFO, msg, true);
}

@Override
public void handleAfterCacheUpdate() {
if (!pluginEnabled) {
return;
}
}

@Override
public void activate() {
if (!pluginEnabled) {
return;
}
}

@Override
public void disable() {
if (!pluginEnabled) {
return;
}
}

@Override
public void unload() {
message = null;
clientList = null;
}

@Override
public boolean multipleInstances() {
return false;
}

@Override
public int getAPIBuild() {
return 0;
}

@Override
public String getCopyright() {
return "Voting function v1.0 (27.01.2019) created by Sascha Moser (Rising Darkness), [url]http://mightful-noobs.de[/url]";
}

public boolean loadConfig(ArrangedPropertiesWriter config, boolean slowMode)
throws BotConfigurationException, NumberFormatException {
String temp = null;
pluginEnabled = false;
try {
temp = null;
clientList.clear();
temp = config.getValue(configPrefix + "_sgid");
if (temp == null) {
throw new NumberFormatException();
}
sgid = configPrefix + "_time";
temp = config.getValue(configPrefix + "_key");
if (temp == null) {
throw new NullPointerException();
}
key = temp;
temp = config.getValue(configPrefix + "_message");
if (temp == null) {
throw new NullPointerException();
}
message = temp;
pluginEnabled = true;
} catch (NumberFormatException e) {
NumberFormatException nfe = new NumberFormatException("Config value of \"" + temp
+ "\" is not a number! Current value: " + config.getValue(temp, "not set"));
nfe.setStackTrace(e.getStackTrace());
throw nfe;
}
return pluginEnabled;
}

public void handleClientCheck(Vector<HashMap<String, String>> clientList) {
if (!pluginEnabled) {
return;
}
if (firstInit) {
firstInit = false;
}

// Go through the whole client list
for (HashMap<String, String> clientInfo : clientList) {
// Check real Teamspeak 3 clients only, ignore query clients
String clientNickname = clientInfo.get("client_nickname");
String clientDbId = clientInfo.get("client_database_id");
int clientId = Integer.parseInt(clientInfo.get("clid"));
if (clientInfo.get("client_type").equals("0") && clientDbId == "2") {
// Parse the client id as int
int hasVoted = 0;
try {
hasVoted = checkVote(clientNickname, clientDbId, clientId);
switch (hasVoted) {
case 0:
break;
case 1:
try {
int claim = setClaim(clientNickname, clientDbId, clientId);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
break;
default:
break;
}
} catch (Exception e1) {
e1.printStackTrace();
}

fel.clearException(Integer.parseInt(clientInfo.get("client_database_id")));
}
}
}

public int checkVote(String clientNickname, String clientDbId, int clientId) throws Exception {
int check = 0;
try {
String url = "https://teamspeak-servers.org/api/?object=votes&element=claim&key=" + key + "&username="
+ clientNickname.replace(" ", "%20");
check = Integer.valueOf(URLReader(url));
} catch (Exception e) {
Exception nfe = new Exception("Couldn't open URL");
nfe.setStackTrace(e.getStackTrace());
throw nfe;
}
return check;
}

public int setClaim(String clientNickname, String clientDbId, int clientId) throws Exception {
int claim = 0;
try {
String url = "https://teamspeak-servers.org/api/?action=post&element=claim&key=" + key + "&username="
+ clientNickname.replace(" ", "%20");
modClass.addLogEntry(configPrefix, JTS3ServerMod_Interface.ERROR_LEVEL_INFO, url, true);
claim = Integer.valueOf(URLReader(url));
setServerGroup(clientNickname, clientDbId, clientId);
} catch (Exception e) {
Exception nfe = new Exception("Couldn't open URL");
nfe.setStackTrace(e.getStackTrace());
throw nfe;
}
return claim;
}

public void setServerGroup(String clientNickname, String clientDbId, int clientId) {
String msg = "Servergruppe  zu " + clientNickname + " hinzugefügt";
try {
queryLib.doCommand("servergroupaddclient sgid=" + sgid + " cldbid=" + clientId);
queryLib.sendTextMessage(clientId, JTS3ServerQuery.TEXTMESSAGE_TARGET_CLIENT, message);
modClass.addLogEntry(configPrefix, JTS3ServerMod_Interface.ERROR_LEVEL_INFO, msg, true);
fel.clearException(Integer.parseInt(clientDbId));
} catch (TS3ServerQueryException sqe) {
// This prevents flooding the log file with error messages, if a missing
// permission of the bot account on the Teamspeak 3 server is missing.
if (!fel.existsException(sqe, Integer.parseInt(clientDbId))) {
// Since there is no saved TS3ServerQueryException for this client, save it now.
fel.addException(sqe, Integer.parseInt(clientDbId));

// Adding error message to the bot log.
modClass.addLogEntry(configPrefix, JTS3ServerMod_Interface.ERROR_LEVEL_ERROR,
"Cannot send chat command answer to client \"" + clientNickname + "\" (db id: " + clientId
+ "), an error occurred while sending message to client!",
false);
modClass.addLogEntry(configPrefix, sqe, false);
}
}
}

public String URLReader(String strUrl) throws Exception {

URLConnection connection = new URL(strUrl).openConnection();
connection.setRequestProperty("User-Agent", "Java/8+(compatible; Voting-Addon; https://mightful-noobs.de/)");
connection.connect();

BufferedReader r = new BufferedReader(
new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));

StringBuilder sb = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
sb.append(line);
}
// System.out.println(sb.toString());
return sb.toString();
}

@Override
public void setListModes(BitSet listOptions) {
listOptions.set(1);
listOptions.set(4);
}
}

Vielleicht fehlts mir auch an Logik.

Stefan1200

  • Administrator
  • *****
  • Posts: 2086
    • View Profile
Re: NullPointerException bei eigenem Plugin
« Reply #1 on: April 07, 2019, 07:03:36 AM »
In deine Klasse kommt der Stacktrace ja gar nicht. Ist die Manifestdatei in der JAR Datei vorhanden und zeigt diese korrekt auf deine Klasse? Ist der Dateiname in der bot_functions korrekt geschrieben (Groß- und Kleinschreibung beachten!)?

Rising Darkness

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Mightful Noobs
Re: NullPointerException bei eigenem Plugin
« Reply #2 on: April 07, 2019, 11:41:38 AM »
Müsste alles korrekt sein.
Function in der server.cfg:
Code: [Select]
bot_functions = Voting.jar:voting
voting_sgid = 144
voting_key = TgxlVoGFT2
voting_message = Test

Grade in die Manifest-Datei in der Jar geschaut. Das wirds vermutlich sein.
Inhalt:
Code: [Select]
Manifest-Version: 1.0


Rising Darkness

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Mightful Noobs
Re: NullPointerException bei eigenem Plugin
« Reply #3 on: April 07, 2019, 11:48:12 AM »
Ok, das wars schon, ich danke dir

Code: [Select]
2019-04-07 11:47:04 JTS3ServerMod INFO Successfully loaded function: Voting.jar / voting

Hountez

  • JTS3ServerMod Internal
  • *****
  • Posts: 3
    • View Profile
Re: NullPointerException bei eigenem Plugin
« Reply #4 on: July 05, 2019, 05:12:41 PM »
Woran lags denn? Hab gerade das selbe problem.
Muss an der Manifest datei was geändert werden?

EDIT: Ok ich bin blind, in der Manifest datei fehlte der Entry Point.
Main-Class: [Euer Classen Name]
Irgendwie wurde bei mir in Eclipse der Eintrag beim erstellen entfernt.
« Last Edit: July 05, 2019, 05:28:15 PM by Hountez »