0

i try made config with Gson i made example this code:

    Channel c = new Channel(3, "TEST STRING!");
    Gson gson = new GsonBuilder().create();

    FileWriter writer = new FileWriter("config.json");

    gson.toJson(c, writer);

    writer.flush();

    Reader reader = new InputStreamReader(Bot.class.getResourceAsStream("config.json"), "UTF - 8");

    Gson ab = new GsonBuilder().create();

    Channel a = ab.fromJson(reader, Channel.class);

    System.out.println(a);

but i have error: java.lang.NullPointerException in line

Reader reader = new InputStreamReader(Bot.class.getResourceAsStream("config.json"), "UTF - 8");

where i made mistake?

other question, how to make config in file?

Larry
  • 101
  • 9
  • ^^^ Essentially boils down to the specified resource not being found (and that way you get a NPE there) – randers Dec 27 '15 at 22:23
  • Oh, now I see. You are writing to a file, but that file is not on the classpath. To load that config file, either load it like you would with a normal file (`new FileInputStream("config.json")` instead of `getResource()`) or place it (not really possible during runtime) in development at the root of your `src` directory (that way it will be present on the classpath, but you won't be able to write it at runtime - it will have to be there even before the program is run) – randers Dec 27 '15 at 22:28
  • @RAnders00 can you give me example code to this? – Larry Dec 28 '15 at 00:03
  • Tomorrow (for me it's 01:04) – randers Dec 28 '15 at 00:04
  • me too i will wait:) good night :) – Larry Dec 28 '15 at 00:08

1 Answers1

1

Option one: The file you are reading cannot be created at runtime and you create it by hand in your development environment, just like you would create a java source file. That way it can be on the classpath, available to you via getResourceAsStream():

Project layout:

MyProject
└── src
    └── config.json // option one
    └── com
        └── myproject
            └── Main.java
            └── config.json // option two

Your code:

// If you placed config.json at location 1 (notice the leading slash)
Reader reader = new InputStreamReader(Bot.class.getResourceAsStream("/config.json"), "UTF - 8");
// If you placed config.json at location 2 (notice no leading slash)
Reader reader = new InputStreamReader(Bot.class.getResourceAsStream("config.json"), "UTF - 8");

Gson ab = new GsonBuilder().create();

Channel a = ab.fromJson(reader, Channel.class);

System.out.println(a);

Option two: This is probably the solution to your question, but I figured I would clarify what getResourceAsStream() does. Instead of trying to find config.json on the classpath, load it again from the file you just saved it to.

// You might need to add this import
import java.nio.charset.StandardCharsets;

/*
 * THIS BLOCK SAVES THE `Channel` INSTANCE TO THE FILE `config.json`
 */
// I also fixed this. Always specify your encodings!
try(FileOutputStream fos = new FileOutputStream("config.json");
    OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
    BufferedWriter writer = new BufferedWriter(osw))
{
    Channel c = new Channel(3, "TEST STRING!");
    Gson gson = new GsonBuilder().create();
    gson.toJson(c, writer);
} // This is a "try-with-resources" statement. The Closeable resource defined in the try() block will automatically be closed at the end of the try block.

/*
 * THIS BLOCK RECONSTRUCTS THE JUST SAVED `Channel` instance from the file `config.json`
 */
// This opens the file 'config.json' in the working directory of the running program
try(FileInputStream fis = new FileInputStream("config.json");
    InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
    BufferedReader reader = new BufferedReader(isr))
{
    Gson ab = new GsonBuilder().create();
    Channel a = ab.fromJson(reader, Channel.class);
    System.out.println(a);
} // Again, a try-with-resources statement
randers
  • 5,031
  • 5
  • 37
  • 64
  • Hello @Larry, I would appreciate it if you could click the checkmark next to my answer if it helped you :) – randers Dec 29 '15 at 22:47