Hey, So I probably have done something stupid yet small and I'm just missing it. When my plugin runs, this error shows up in the console. Code: [Server thread/INFO]: java.lang.NullPointerException at net.WoodyCraft.Downloader.FTPUploader.connect(FTPUploader.java:112) Where the error occurs.. Code:java try { // Error Here // (Line 3) String directoryLoc = Plugin.getConfig().getString("Credentials.Directory"); URL URL = new URL("ftp://" + Username + ":" + Password + "@" + Host + "/" + directoryLoc + "/" + remoteFile + ";type=i"); Client = URL.openConnection(); return true; } catch (Exception e) { Top of the class.. Code:java public Main Plugin; public FTPUploader(Main Plugin){ this.Plugin = Plugin; } I know the Error is related to calling the config, because when I remove the line, no error appears. I just need to know exactly what is wrong and how I can fix it.
Is Plugin null? It's a null pointer exception I mean, is the line with the error called before a plugin is initialized in your onenable? Or it isn't initialized? An we see full code? EDIT by Moderator: merged posts, please use the edit button instead of double posting.
FTPDownloader.java Code:java package net.WoodyCraft.Downloader; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.InputStream;import java.i:confused:utputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.net.URL;import java.net.URLConnection; public class FTPUploader { public Main Plugin; public FTPUploader(Main Plugin){this.Plugin = Plugin;} public FTPUploader() { } private URLConnection Client;private String Host;private String Username;private String Password;private String remoteFile;private String Error;private String Success; public void FTPClient(String Host, String Username, String Password) { this.Host = Host;this.Username = Username;this.Password = Password; } public void setHost(String string) { this.Host = string;} public void setUser(String Username) { this.Username = Username;} public void setPassword(String Password) { this.Password = Password;} public void setRemoteFile(String File) { this.remoteFile = File;} public synchronized String getSucMsg() { if(Success != null)return Success;return null;} public synchronized String getErrMsg() { if(Error != null)return Error;return null;} public synchronized boolean uploadFile(String fileLoc) {try {InputStream Input = new FileInputStream(fileLoc);BufferedInputStream BufferedIn = new BufferedInputStream(Input); OutputStream Output = Client.getOutputStream();BufferedOutputStream BufferedOut = new BufferedOutputStream(Output); byte[] Buffer = new byte[1024]; int count; while((count = BufferedIn.read(Buffer)) >0 ) BufferedOut.write(Buffer, 0, count);BufferedOut.close(); return true; } catch (Exception Ex) { StringWriter SWriter = new StringWriter();PrintWriter PWriter = new PrintWriter(SWriter, true);Ex.printStackTrace(PWriter);Error = SWriter.getBuffer().toString(); return false;}} public synchronized boolean connect() {try {String directoryLoc = Plugin.getConfig().getString("Credentials.Directory");URL URL = new URL("ftp://" + Username + ":" + Password + "@" + Host + "/" + directoryLoc + "/" + remoteFile + ";type=i"); Client = URL.openConnection();return true; } catch (Exception Ex) { StringWriter SWriter = new StringWriter();PrintWriter PWriter = new PrintWriter(SWriter, true);Ex.printStackTrace(PWriter);Error = SWriter.getBuffer().toString(); return false;}}} Main.java Code:java package net.WoodyCraft.Downloader; import java.io.File; import org.bukkit.Bukkit;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin { public void onEnable() { saveDefaultConfig(); System.out.print("[WoodyCraft-Downloader] Successfully Started!"); uploadSchemFile("Test.txt"); } public void uploadSchemFile(String Schematic) { File dataFolder = Bukkit.getPluginManager().getPlugin("WoodyCraft-Downloader").getDataFolder(); FTPUploader FTP = new FTPUploader(); FTP.setHost(getConfig().getString("Credentials.Host")); FTP.setUser(getConfig().getString("Credentials.Username")); FTP.setPassword(getConfig().getString("Credentials.Password")); FTP.setRemoteFile(Schematic); FTP.uploadFile(new File(dataFolder, Schematic).getPath()); if(FTP.connect()) if(FTP.uploadFile(new File(dataFolder, Schematic).getPath())) System.out.print("File Upload Success!"); else System.out.println(FTP.getErrMsg()); else System.out.println(FTP.getErrMsg()); }}
I think you may want to initialize plugin...like Plugin plugin; // plugin object (...) public void onEnable(){ plugin=this; //Then the other stuff... } And then pass the plugin object to other classes. In your case as I see you call the class plugin without initializing it. Hope I helped
Thanks I'll try it out I added it so now my code looks like this.. Main.java Code:java package com.Culaccino.Downloader; import java.io.File; import org.bukkit.Bukkit;import org.bukkit.plugin.Plugin;import org.bukkit.plugin.java.JavaPlugin; public class Main extends JavaPlugin { Plugin Plugin; public void onEnable() { Plugin = this;saveDefaultConfig();System.out.print("[Plot-Downloader] Successfully Started!"); uploadSchemFile("Test.txt");} public void uploadSchemFile(String Schematic) { File dataFolder = Bukkit.getPluginManager().getPlugin("Plot-Downloader").getDataFolder(); FTPUploader FTP = new FTPUploader(); FTP.setHost(getConfig().getString("Server.Host"));FTP.setUser(getConfig().getString("Server.Username"));FTP.setPassword(getConfig().getString("Server.Password"));FTP.setRemoteFile(Schematic); FTP.uploadFile(new File(dataFolder, Schematic).getPath()); if(FTP.connect())if(FTP.uploadFile(new File(dataFolder, Schematic).getPath()))System.out.print("[Plot-Downloader] File Upload Success!");elseSystem.out.println(FTP.getErrMsg());elseSystem.out.println(FTP.getErrMsg());}} FTPUploader.java Code:java package com.Culaccino.Downloader; import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.InputStream;import java.i:confused:utputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.net.URL;import java.net.URLConnection; public class FTPUploader { public Main Plugin; public FTPUploader(Main Plugin){this.Plugin = Plugin;} public FTPUploader() { } private URLConnection Client;private String Host;private String Username;private String Password;private String remoteFile;private String Error;private String Success; public void FTPClient(String Host, String Username, String Password) { this.Host = Host;this.Username = Username;this.Password = Password; } public void setHost(String string) { this.Host = string;} public void setUser(String Username) { this.Username = Username;} public void setPassword(String Password) { this.Password = Password;} public void setRemoteFile(String File) { this.remoteFile = File;} public synchronized String getSucMsg() { if(Success != null)return Success;return null;} public synchronized String getErrMsg() { if(Error != null)return Error;return null;} public synchronized boolean uploadFile(String fileLoc) {try {InputStream Input = new FileInputStream(fileLoc);BufferedInputStream BufferedIn = new BufferedInputStream(Input); OutputStream Output = Client.getOutputStream();BufferedOutputStream BufferedOut = new BufferedOutputStream(Output); byte[] Buffer = new byte[1024]; int count; while((count = BufferedIn.read(Buffer)) >0 ) BufferedOut.write(Buffer, 0, count);BufferedOut.close(); return true; } catch (Exception Ex) { StringWriter SWriter = new StringWriter();PrintWriter PWriter = new PrintWriter(SWriter, true);Ex.printStackTrace(PWriter);Error = SWriter.getBuffer().toString(); return false;}} public synchronized boolean connect() {try {String directoryLoc = Plugin.getConfig().getString("Server.Directory");URL URL = new URL("ftp://" + Username + ":" + Password + "@" + Host + "/" + directoryLoc + "/" + remoteFile + ";type=i"); Client = URL.openConnection();return true; } catch (Exception Ex) { StringWriter SWriter = new StringWriter();PrintWriter PWriter = new PrintWriter(SWriter, true);Ex.printStackTrace(PWriter);Error = SWriter.getBuffer().toString(); return false;}}} I still get the same error... Code: [Server thread/INFO]: java.lang.NullPointerException at com.Culaccino.Downloader.FTPUploader.connect(FTPUploader.java:107) I also have the config.yml setup aswell.. Code: Server: Directory: '' Host: '' Username: '' Password: '' I actually have stuff in the config blanks, I just don't need my web-server details public haha EDIT by Moderator: merged posts, please use the edit button instead of double posting.