当前位置: 首页 > 知识库问答 >
问题:

带有Java的JSON中出现错误:主线程中出现异常

苏鸿羽
2023-03-14

我是Java公司的JSON新手,并试图使用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)  

谁能帮我找出哪里不对劲。

共有1个答案

齐嘉庆
2023-03-14

此行的CreateParser调用中的问题(NullPointerException):

JsonParser parser = Json.createParser(
    ReadJSONFile.class.getResourceAsStream("/person.json"));

它的发生是因为

ReadJSONFile.class.getResourceAsStream("/person.json")

正在计算为

如果您查看class.getResourceasStream的javadoc,您将看到当类类加载器在类加载器的命名空间中找不到命名资源时,返回null

由于您使用的是Maven,所以它要查找的文件需要在项目的“src/main/resources”目录中;有关详细信息,请参阅标准目录布局简介。

将JSON文件放在文件系统中并使用fileinputstream打开它可能是一个更好的主意。在这里使用资源的问题是,您的应用程序将只能读取嵌入在应用程序中的JSON文件;例如在其JAR文件中。

 类似资料: