getConfig() Error

Discussion in 'Plugin Development' started by Vaact, Aug 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Vaact

    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
    1. try {
    2. // Error Here // (Line 3)
    3. String directoryLoc = Plugin.getConfig().getString("Credentials.Directory");
    4. URL URL = new URL("ftp://" + Username + ":" + Password + "@" + Host + "/" + directoryLoc + "/" + remoteFile + ";type=i");
    5.  
    6. Client = URL.openConnection();
    7. return true;
    8. } catch (Exception e) {

    Top of the class..
    Code:java
    1. public Main Plugin;
    2.  
    3. public FTPUploader(Main Plugin){
    4. this.Plugin = Plugin;
    5. }

    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.
     
  2. Offline

    mine-care

    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.
     
    Last edited by a moderator: Jun 10, 2016
  3. Offline

    Vaact

    FTPDownloader.java
    Code:java
    1. package net.WoodyCraft.Downloader;
    2.  
    3. import java.io.BufferedInputStream;
    4. import java.io.BufferedOutputStream;
    5. import java.io.FileInputStream;
    6. import java.io.InputStream;
    7. import java.i:confused:utputStream;
    8. import java.io.PrintWriter;
    9. import java.io.StringWriter;
    10. import java.net.URL;
    11. import java.net.URLConnection;
    12.  
    13. public class FTPUploader {
    14.  
    15. public Main Plugin;
    16.  
    17. public FTPUploader(Main Plugin){
    18. this.Plugin = Plugin;
    19. }
    20.  
    21. public FTPUploader() {
    22.  
    23. }
    24.  
    25. private URLConnection Client;
    26. private String Host;
    27. private String Username;
    28. private String Password;
    29. private String remoteFile;
    30. private String Error;
    31. private String Success;
    32.  
    33. public void FTPClient(String Host, String Username, String Password) {
    34.  
    35. this.Host = Host;
    36. this.Username = Username;
    37. this.Password = Password;
    38.  
    39. }
    40.  
    41. public void setHost(String string) {
    42.  
    43. this.Host = string;
    44. }
    45.  
    46. public void setUser(String Username) {
    47.  
    48. this.Username = Username;
    49. }
    50.  
    51. public void setPassword(String Password) {
    52.  
    53. this.Password = Password;
    54. }
    55.  
    56. public void setRemoteFile(String File) {
    57.  
    58. this.remoteFile = File;
    59. }
    60.  
    61. public synchronized String getSucMsg() {
    62.  
    63. if(Success != null)
    64. return Success;
    65. return null;
    66. }
    67.  
    68. public synchronized String getErrMsg() {
    69.  
    70. if(Error != null)
    71. return Error;
    72. return null;
    73. }
    74.  
    75. public synchronized boolean uploadFile(String fileLoc) {
    76. try {
    77. InputStream Input = new FileInputStream(fileLoc);
    78. BufferedInputStream BufferedIn = new BufferedInputStream(Input);
    79.  
    80. OutputStream Output = Client.getOutputStream();
    81. BufferedOutputStream BufferedOut = new BufferedOutputStream(Output);
    82.  
    83. byte[] Buffer = new byte[1024];
    84.  
    85. int count;
    86.  
    87. while((count = BufferedIn.read(Buffer)) >0 )
    88.  
    89. BufferedOut.write(Buffer, 0, count);
    90. BufferedOut.close();
    91.  
    92. return true;
    93.  
    94. } catch (Exception Ex) {
    95.  
    96. StringWriter SWriter = new StringWriter();
    97. PrintWriter PWriter = new PrintWriter(SWriter, true);
    98. Ex.printStackTrace(PWriter);
    99. Error = SWriter.getBuffer().toString();
    100.  
    101. return false;
    102. }
    103. }
    104.  
    105. public synchronized boolean connect() {
    106. try {
    107. String directoryLoc = Plugin.getConfig().getString("Credentials.Directory");
    108. URL URL = new URL("ftp://" + Username + ":" + Password + "@" + Host + "/" + directoryLoc + "/" + remoteFile + ";type=i");
    109.  
    110. Client = URL.openConnection();
    111. return true;
    112.  
    113. } catch (Exception Ex) {
    114.  
    115. StringWriter SWriter = new StringWriter();
    116. PrintWriter PWriter = new PrintWriter(SWriter, true);
    117. Ex.printStackTrace(PWriter);
    118. Error = SWriter.getBuffer().toString();
    119.  
    120. return false;
    121. }
    122. }
    123. }


    Main.java
    Code:java
    1. package net.WoodyCraft.Downloader;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.plugin.java.JavaPlugin;
    7.  
    8. public class Main extends JavaPlugin {
    9.  
    10. public void onEnable() {
    11.  
    12. saveDefaultConfig();
    13.  
    14. System.out.print("[WoodyCraft-Downloader] Successfully Started!");
    15.  
    16. uploadSchemFile("Test.txt");
    17.  
    18. }
    19.  
    20. public void uploadSchemFile(String Schematic) {
    21.  
    22. File dataFolder = Bukkit.getPluginManager().getPlugin("WoodyCraft-Downloader").getDataFolder();
    23.  
    24. FTPUploader FTP = new FTPUploader();
    25.  
    26. FTP.setHost(getConfig().getString("Credentials.Host"));
    27. FTP.setUser(getConfig().getString("Credentials.Username"));
    28. FTP.setPassword(getConfig().getString("Credentials.Password"));
    29. FTP.setRemoteFile(Schematic);
    30.  
    31. FTP.uploadFile(new File(dataFolder, Schematic).getPath());
    32.  
    33. if(FTP.connect())
    34. if(FTP.uploadFile(new File(dataFolder, Schematic).getPath()))
    35. System.out.print("File Upload Success!");
    36. else
    37. System.out.println(FTP.getErrMsg());
    38. else
    39. System.out.println(FTP.getErrMsg());
    40. }
    41. }

     
  4. Offline

    mine-care

    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 :)
     
  5. Offline

    Vaact


    Thanks I'll try it out :)


    I added it so now my code looks like this..

    Main.java
    Code:java
    1. package com.Culaccino.Downloader;
    2.  
    3. import java.io.File;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.plugin.Plugin;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class Main extends JavaPlugin {
    10.  
    11. Plugin Plugin;
    12.  
    13. public void onEnable() {
    14.  
    15. Plugin = this;
    16. saveDefaultConfig();
    17. System.out.print("[Plot-Downloader] Successfully Started!");
    18.  
    19. uploadSchemFile("Test.txt");
    20. }
    21.  
    22. public void uploadSchemFile(String Schematic) {
    23.  
    24. File dataFolder = Bukkit.getPluginManager().getPlugin("Plot-Downloader").getDataFolder();
    25.  
    26. FTPUploader FTP = new FTPUploader();
    27.  
    28. FTP.setHost(getConfig().getString("Server.Host"));
    29. FTP.setUser(getConfig().getString("Server.Username"));
    30. FTP.setPassword(getConfig().getString("Server.Password"));
    31. FTP.setRemoteFile(Schematic);
    32.  
    33. FTP.uploadFile(new File(dataFolder, Schematic).getPath());
    34.  
    35. if(FTP.connect())
    36. if(FTP.uploadFile(new File(dataFolder, Schematic).getPath()))
    37. System.out.print("[Plot-Downloader] File Upload Success!");
    38. else
    39. System.out.println(FTP.getErrMsg());
    40. else
    41. System.out.println(FTP.getErrMsg());
    42. }
    43. }


    FTPUploader.java
    Code:java
    1. package com.Culaccino.Downloader;
    2.  
    3. import java.io.BufferedInputStream;
    4. import java.io.BufferedOutputStream;
    5. import java.io.FileInputStream;
    6. import java.io.InputStream;
    7. import java.i:confused:utputStream;
    8. import java.io.PrintWriter;
    9. import java.io.StringWriter;
    10. import java.net.URL;
    11. import java.net.URLConnection;
    12.  
    13. public class FTPUploader {
    14.  
    15. public Main Plugin;
    16.  
    17. public FTPUploader(Main Plugin){
    18. this.Plugin = Plugin;
    19. }
    20.  
    21. public FTPUploader() {
    22.  
    23. }
    24.  
    25. private URLConnection Client;
    26. private String Host;
    27. private String Username;
    28. private String Password;
    29. private String remoteFile;
    30. private String Error;
    31. private String Success;
    32.  
    33. public void FTPClient(String Host, String Username, String Password) {
    34.  
    35. this.Host = Host;
    36. this.Username = Username;
    37. this.Password = Password;
    38.  
    39. }
    40.  
    41. public void setHost(String string) {
    42.  
    43. this.Host = string;
    44. }
    45.  
    46. public void setUser(String Username) {
    47.  
    48. this.Username = Username;
    49. }
    50.  
    51. public void setPassword(String Password) {
    52.  
    53. this.Password = Password;
    54. }
    55.  
    56. public void setRemoteFile(String File) {
    57.  
    58. this.remoteFile = File;
    59. }
    60.  
    61. public synchronized String getSucMsg() {
    62.  
    63. if(Success != null)
    64. return Success;
    65. return null;
    66. }
    67.  
    68. public synchronized String getErrMsg() {
    69.  
    70. if(Error != null)
    71. return Error;
    72. return null;
    73. }
    74.  
    75. public synchronized boolean uploadFile(String fileLoc) {
    76. try {
    77. InputStream Input = new FileInputStream(fileLoc);
    78. BufferedInputStream BufferedIn = new BufferedInputStream(Input);
    79.  
    80. OutputStream Output = Client.getOutputStream();
    81. BufferedOutputStream BufferedOut = new BufferedOutputStream(Output);
    82.  
    83. byte[] Buffer = new byte[1024];
    84.  
    85. int count;
    86.  
    87. while((count = BufferedIn.read(Buffer)) >0 )
    88.  
    89. BufferedOut.write(Buffer, 0, count);
    90. BufferedOut.close();
    91.  
    92. return true;
    93.  
    94. } catch (Exception Ex) {
    95.  
    96. StringWriter SWriter = new StringWriter();
    97. PrintWriter PWriter = new PrintWriter(SWriter, true);
    98. Ex.printStackTrace(PWriter);
    99. Error = SWriter.getBuffer().toString();
    100.  
    101. return false;
    102. }
    103. }
    104.  
    105. public synchronized boolean connect() {
    106. try {
    107. String directoryLoc = Plugin.getConfig().getString("Server.Directory");
    108. URL URL = new URL("ftp://" + Username + ":" + Password + "@" + Host + "/" + directoryLoc + "/" + remoteFile + ";type=i");
    109.  
    110. Client = URL.openConnection();
    111. return true;
    112.  
    113. } catch (Exception Ex) {
    114.  
    115. StringWriter SWriter = new StringWriter();
    116. PrintWriter PWriter = new PrintWriter(SWriter, true);
    117. Ex.printStackTrace(PWriter);
    118. Error = SWriter.getBuffer().toString();
    119.  
    120. return false;
    121. }
    122. }
    123. }


    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 :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 10, 2016
  6. Offline

    mine-care

    Is there a config appearing in the folder? Does it contain this string you askit for?
     
  7. Offline

    Vaact


    Yeah, the config is generated with the string I want.

    Still displays the error..
     
  8. Offline

    mine-care

    :O mabe you need to ask @fireblast he is a proooooo! i cant see anything else wromg :/
     
Thread Status:
Not open for further replies.

Share This Page