2017-08-22 4 views
4

Ich verwende PropertyUtils.setSimpleProperty, um meine Setter-Methode dynamisch aufzurufen, aber aus irgendeinem Grund bleibe ich eingeschaltet Fehler bekommen. Brauchen Sie Ihre Hilfe, um die Ursache herauszufinden. Hier ist mein Code:Abrufen von java.lang.NoSuchMethodException: Eigenschaft 'xx' hat keine Setter-Methode in Klasse 'Klasse xx' bei Verwendung der Funktion PropertyUtils.setSimpleProperty

class FileDt { 
    String reportName=null; 
    String reportLocation=null; 

    public String getReportName() { 
     return reportName; 
    } 
    public void setReportName(String reportName) { 
     this.reportName = reportName; 
    } 
    public String getReportLocation() { 
     return reportLocation; 
    } 
    public void setReportLocation(String reportLocation) { 
     this.reportLocation = reportLocation; 
    } 
} 

class Foo { 
    public static void main (String... args) { 
     FileDt dt = newFileDt(); 
     // #1 
     PropertyUtilsBean.setSimpleProperty(dt, "reportName", "abc.html"); 
     // #2 
     PropertyUtilsBean.setSimpleProperty(dt, "reportLocation", "c://"); 
    } 
} 

Beide Methoden werfen Ausnahme

  1. Verursacht durch: java.lang.NoSuchMethodException: Anwesen 'REPORT' hat keine Setter-Methode in der Klasse ‚Klasse FileDt 'bei org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty (PropertyUtilsBean.java:2096)

  2. Verursacht durch: java.lang.NoSuchMethodException: Anwesen 'report' hat keine Setter-Methode in der Klasse 'Klasse FileDt' bei org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty (PropertyUtilsBean.java:2096)

+0

Hatte die Antwort, die ich zur Verfügung gestellt habe dein Problem lösen? –

Antwort

3

PropertyUtilsBean.setSimpleProperty(Object bean, String name, Object value)) arbeitet nur mit öffentlichen Methoden. Es sieht so aus, als ob Ihre Klasse den Paketbereich verwendet (das Schlüsselwort public in der Klassendefinition fehlt).

Lauf folgendes Beispiel:

import org.apache.commons.beanutils.PropertyUtils; 

import java.lang.reflect.InvocationTargetException; 

class FileDt { 
    String reportName; 
    String reportLocation; 

    public String getReportName() { 
     return reportName; 
    } 

    public void setReportName(String reportName) { 
     this.reportName = reportName; 
    } 

    public String getReportLocation() { 
     return reportLocation; 
    } 

    public void setReportLocation(String reportLocation) { 
     this.reportLocation = reportLocation; 
    } 

    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { 
     FileDt dt = new FileDt(); 
     // #1 
     PropertyUtils.setSimpleProperty(dt, "reportName", "abc.html"); 
     // #2 
     PropertyUtils.setSimpleProperty(dt, "reportLocation", "c://"); 
    } 
} 

löst eine Ausnahme, die Sie beschrieben haben:

Exception in thread "main" java.lang.NoSuchMethodException: Property 'reportName' has no setter method in class 'class FileDt' 
    at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096) 
    at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.java:928) 
    at FileDt.main(FileDt.java:28) 

Erstellen der Klasse public löst das Problem:

import org.apache.commons.beanutils.PropertyUtils; 

import java.lang.reflect.InvocationTargetException; 

public class FileDt { 
    String reportName; 
    String reportLocation; 

    public String getReportName() { 
     return reportName; 
    } 

    public void setReportName(String reportName) { 
     this.reportName = reportName; 
    } 

    public String getReportLocation() { 
     return reportLocation; 
    } 

    public void setReportLocation(String reportLocation) { 
     this.reportLocation = reportLocation; 
    } 

    public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { 
     FileDt dt = new FileDt(); 
     // #1 
     PropertyUtils.setSimpleProperty(dt, "reportName", "abc.html"); 
     // #2 
     PropertyUtils.setSimpleProperty(dt, "reportLocation", "c://"); 
    } 
} 
Verwandte Themen