Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在执行时,我面临Java.lang.nosuchFeildExecption_Java - Fatal编程技术网

在执行时,我面临Java.lang.nosuchFeildExecption

在执行时,我面临Java.lang.nosuchFeildExecption,java,Java,我正面临这个Java.lang.nosuchFeildExecption 在执行代码时。。 谁能帮我这个忙,怎么才能解决这个问题?? 提前谢谢 我正面临这个Java.lang.nosuchFeildExecption 在执行代码时 public class Validation { public void readJSON() throws Exception { File file = new File("myJSONFile.txt");

我正面临这个Java.lang.nosuchFeildExecption 在执行代码时。。 谁能帮我这个忙,怎么才能解决这个问题?? 提前谢谢 我正面临这个Java.lang.nosuchFeildExecption 在执行代码时

    public class Validation {
    public void readJSON() throws Exception {
            File file = new File("myJSONFile.txt");
            String content = FileUtils.readFileToString(file, "utf-8");

            // Convert JSON string to JSONObject
            JSONObject tomJsonObject = new JSONObject(content);
            System.out.println(tomJsonObject);
            System.out.println(tomJsonObject.getString("age"));
            validateByType(tomJsonObject, "age", null);
        }

        public void validateByType(JSONObject jsonString, String pathString, String typeString)
                throws JSONException, ClassNotFoundException, NoSuchFieldException, SecurityException {
            String jsonField = jsonString.getString(pathString);
            // pathString = "age";
            // typeString ="number";

            Class<?> c = Class.forName("Validation");
            Field f = c.getField(jsonField);
            System.out.format("Type: %s%n", f.getType());
            System.out.format("GenericType: %s%n", f.getGenericType());

        }

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            System.out.println("Test");
            Validation v = new Validation();
            try {
                v.readJSON();

            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
让路 例如:File File=新文件:\myJSONFile.txt; 或者根据您的要求给出您的路径 在windows机器中 字符串absoluteFilePath=fileSeparator+Users+fileSeparator+roopesh+fileSeparator+myJSONFile.txt;
File File=新文件绝对文件路径

可以将该文件添加到src/main/resources文件夹,然后使用java.nio包读取该文件:

 Path path = Path.get(getClass().getClassLoader().getResource("myJSONFile.txt").toURI());
 byte[] fileBytes = Files.readAllBytes(path);
 String theString = new String(fileBytes, "UTF-8");
现在阅读类和字段:

 Class<?> c = Class.forName("<give class name with package>");

 //whatever field is attempted to be read
 Field f = c.getDeclaredField("fieldName");

很明显,系统无法找到myJSONFile.txt文件,请尝试将json文件放在project的resources文件夹中,并使用相对路径;字段f=c.getFieldjsonField;System.out.formatType:%s%n,f.getType;System.out.formatGenericType:%s%n,f.getGenericType;如果他决定在另一台机器上运行它,那是行不通的。我只举了一个例子。。根据他的机器或项目结构要求,他应该给出路径,例如:在windows机器中::String absoluteFilePath=fileSeparator+Users+fileSeparator+roopesh+fileSeparator+file.txt;File File=新文件绝对文件路径;是的,我只给出了这条路径,但问题在于——c类=Class.forNameValidation;字段f=c.getFieldjsonField;System.out.formatType:%s%n,f.getType;System.out.formatGenericType:%s%n,f.getGenericType;是的,我只给出了这条路径,但问题在于——c类=Class.forNameValidation;字段f=c.getFieldjsonField;System.out.formatType:%s%n,f.getType;System.out.formatGenericType:%s%n,f.getGenericType;我更新了答案,看看它是否有助于回答您关于Reflectional的查询。因此,您问题中的异常名称是错误的。然后应该抛出java.lang.NoSuchFieldException;字段f=c.getDeclaredFieldjsonField;System.out.formatType:%s%n,f.getType;System.out.formatGenericType:%s%n,f.getGenericType;}-------但仍然面临同样的问题--java.lang.NoSuchFieldException:age at java.lang.Class.getDeclaredFieldUnknown Source at com.sample.validation.validation.validateByTypeValidation.java:47 at com.sample.validation.validation.readJSONValidation.java:34 at com.sample.validation.validation.mainValidation.java:62的值是多少杰森菲尔德。假设它是否为name是class-com.sample.validation.validation中定义的名称。我在你们班上没有看到任何字段。
 private String fieldName;