JSON 和 java 中的错误:主线程中的异常
Error in JSON with java: Exception in main thread
我是 JSON java 的新手,正在尝试使用 maven 和 eclipse 来探索它。
这是我的 json 文件。
"name": "Jason Bourne",
"profession": "Super agent",
"bad-guy": false,
"kills": 1000,
"phoneNumbers": [
{
"type": "home",
"number": "123-456-789"
},
{
"type": "work",
"number": "123-555-555"
}
]
},
{
"name": "Jason Voorhees",
"profession": "Maniac killer",
"bad-guy": true,
"kills": 100,
"phoneNumbers": [
{
"type": "office",
"number": "666-666-666"
}
]
},
{
"name": "Jason",
"profession": "Lead of Argonauts",
"bad-guy": false,
"kills": null
}
]
我的 pom.xml 文件如下所示
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.assignment</groupId>
<artifactId>assgQuestion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jsonp.version>1.1.2</jsonp.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.json/javax.json-api -->
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>${jsonp.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>${jsonp.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<!-- automatically creates the classpath using all project dependencies,also adding the project build directory -->
<classpath/>
<argument>packt.javaee.json.Main</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
我正在尝试列出所有 jsonp 事件。以下是相同的代码片段。
public class ReadJSONFile {
public static void main(String args[]) {
new ReadJSONFile().start();
}
private void start() {
while(true) {
printOptions();
Scanner sc = new Scanner(System.in);
switch(sc.nextLine()){
case "1" :
jsonParserScenario();
break;
}
}
}
private void printOptions() {
System.out.println("1. JSON Parser");
}
private void jsonParserScenario() {
//Creating a JSONParser object
JsonParser parser = Json.createParser(ReadJSONFile.class.getResourceAsStream("/person.json"));
while (parser.hasNext()) {
//Get event from parse
JsonParser.Event event = parser.next();
//Display event on screen
System.out.println(event.toString());
}
}
}
在 运行 上面的文件之后我收到以下错误。
1. JSON Parser
1
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read()" because "this.in" is null
at org.glassfish.json.UnicodeDetectingInputStream.fillBuf(UnicodeDetectingInputStream.java:89)
at org.glassfish.json.UnicodeDetectingInputStream.detectEncoding(UnicodeDetectingInputStream.java:128)
at org.glassfish.json.UnicodeDetectingInputStream.<init>(UnicodeDetectingInputStream.java:75)
at org.glassfish.json.JsonParserImpl.<init>(JsonParserImpl.java:95)
at org.glassfish.json.JsonProviderImpl.createParser(JsonProviderImpl.java:88)
at javax.json.Json.createParser(Json.java:109)
at assgQuestion.ReadJSONFile.jsonParserScenario(ReadJSONFile.java:52)
at assgQuestion.ReadJSONFile.start(ReadJSONFile.java:29)
at assgQuestion.ReadJSONFile.main(ReadJSONFile.java:19)
谁能帮我找出问题所在。
问题 (NullPointerException
) 发生在 createParser
这条线路上的调用中:
JsonParser parser = Json.createParser(
ReadJSONFile.class.getResourceAsStream("/person.json"));
这是因为表达式
ReadJSONFile.class.getResourceAsStream("/person.json")
正在评估 null
。
如果您查看 Class.getResourceAsStream
的 javadoc,您会看到当 类 类加载器无法在类加载器的命名空间中找到命名资源时返回 null
。
由于您使用的是 Maven,因此它要查找的文件需要位于项目的“src/main/resources”目录中;有关详细信息,请参阅 Introduction to the Standard Directory Layout。
将JSON文件放入文件系统并使用FileInputStream
打开它可能是一个更好的主意。此处使用资源的问题是您的应用程序只能读取应用程序中嵌入的 JSON 文件;例如在它的 JAR 文件中。
我是 JSON java 的新手,正在尝试使用 maven 和 eclipse 来探索它。
这是我的 json 文件。
"name": "Jason Bourne",
"profession": "Super agent",
"bad-guy": false,
"kills": 1000,
"phoneNumbers": [
{
"type": "home",
"number": "123-456-789"
},
{
"type": "work",
"number": "123-555-555"
}
]
},
{
"name": "Jason Voorhees",
"profession": "Maniac killer",
"bad-guy": true,
"kills": 100,
"phoneNumbers": [
{
"type": "office",
"number": "666-666-666"
}
]
},
{
"name": "Jason",
"profession": "Lead of Argonauts",
"bad-guy": false,
"kills": null
}
]
我的 pom.xml 文件如下所示
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.assignment</groupId>
<artifactId>assgQuestion</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jsonp.version>1.1.2</jsonp.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.json/javax.json-api -->
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>${jsonp.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>${jsonp.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<!-- automatically creates the classpath using all project dependencies,also adding the project build directory -->
<classpath/>
<argument>packt.javaee.json.Main</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
我正在尝试列出所有 jsonp 事件。以下是相同的代码片段。
public class ReadJSONFile {
public static void main(String args[]) {
new ReadJSONFile().start();
}
private void start() {
while(true) {
printOptions();
Scanner sc = new Scanner(System.in);
switch(sc.nextLine()){
case "1" :
jsonParserScenario();
break;
}
}
}
private void printOptions() {
System.out.println("1. JSON Parser");
}
private void jsonParserScenario() {
//Creating a JSONParser object
JsonParser parser = Json.createParser(ReadJSONFile.class.getResourceAsStream("/person.json"));
while (parser.hasNext()) {
//Get event from parse
JsonParser.Event event = parser.next();
//Display event on screen
System.out.println(event.toString());
}
}
}
在 运行 上面的文件之后我收到以下错误。
1. JSON Parser
1
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.InputStream.read()" because "this.in" is null
at org.glassfish.json.UnicodeDetectingInputStream.fillBuf(UnicodeDetectingInputStream.java:89)
at org.glassfish.json.UnicodeDetectingInputStream.detectEncoding(UnicodeDetectingInputStream.java:128)
at org.glassfish.json.UnicodeDetectingInputStream.<init>(UnicodeDetectingInputStream.java:75)
at org.glassfish.json.JsonParserImpl.<init>(JsonParserImpl.java:95)
at org.glassfish.json.JsonProviderImpl.createParser(JsonProviderImpl.java:88)
at javax.json.Json.createParser(Json.java:109)
at assgQuestion.ReadJSONFile.jsonParserScenario(ReadJSONFile.java:52)
at assgQuestion.ReadJSONFile.start(ReadJSONFile.java:29)
at assgQuestion.ReadJSONFile.main(ReadJSONFile.java:19)
谁能帮我找出问题所在。
问题 (NullPointerException
) 发生在 createParser
这条线路上的调用中:
JsonParser parser = Json.createParser(
ReadJSONFile.class.getResourceAsStream("/person.json"));
这是因为表达式
ReadJSONFile.class.getResourceAsStream("/person.json")
正在评估 null
。
如果您查看 Class.getResourceAsStream
的 javadoc,您会看到当 类 类加载器无法在类加载器的命名空间中找到命名资源时返回 null
。
由于您使用的是 Maven,因此它要查找的文件需要位于项目的“src/main/resources”目录中;有关详细信息,请参阅 Introduction to the Standard Directory Layout。
将JSON文件放入文件系统并使用FileInputStream
打开它可能是一个更好的主意。此处使用资源的问题是您的应用程序只能读取应用程序中嵌入的 JSON 文件;例如在它的 JAR 文件中。