实体类TestModel,Attachment类也是一个实体类
package com.sinochem.it.model; import com.alibaba.fastjson.JSONObject; public class TestModel { int age; String name; JSONObject obj; Attachment attachment; public Attachment getAttachment() { return attachment; } public void setAttachment(Attachment attachment) { this.attachment = attachment; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public JSONObject getObj() { return obj; } public void setObj(JSONObject obj) { this.obj = obj; } @Override public String toString() { return "TestModel{" + "age=" + age + ", name='" + name + '\'' + ", obj=" + obj + ", attachment=" + attachment + '}'; } }
测试方法:给name,age基础类型赋值没问题,给jsonObject,Attachement这两个引用类型赋值会报空指针.
public static void main(String[] args) { TestModel model = new TestModel(); model.setAge(18); model.setName("Jhon"); //model.getObj().put("name","mack"); model.getAttachment().setBucketName("aa"); System.out.println(model.toString()); }
标签:实体类,java,name,age,报错,attachment,obj,model,public From: https://www.cnblogs.com/1--2/p/17958515