2017-08-22 17 views
4

PropertyUtils.setSimplePropertyを使用してセッターメソッドを動的に呼び出そうとしていますが、エラーが発生します。根本原因を突き止めるためにあなたの助けが必要です。ここに私のコードは次のとおりです。java.lang.NoSuchMethodException::java.lang.NoSuchMethodExceptionの取得:PropertyUtils.setSimpleProperty関数を使用しているときにクラス 'class xx'にプロパティ 'xx'が設定されていません

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://"); 
    } 
} 

どちらの方法でも例外によって引き起こさ

  1. を投げるプロパティ 'REPORTNAME' は、クラスのクラスFileDtにはsetterメソッドを持っていません「 でorg.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)

  2. に起因

    :java.lang.NoSuchMethodException:プロパティ 'reportLocation' はorg.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2096)

    でクラスのクラスFileDt 'にはセッターメソッドを持っていません
+0

は私が提供してきました答えをしましたあなたの問題を解決する? –

答えて

3

PropertyUtilsBean.setSimpleProperty(Object bean, String name, Object value))はパブリックメソッドのみで動作します。あなたのクラスがパッケージスコープを使用しているようです(クラス定義にpublicキーワードがありません)。たとえば、次の

実行:

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://"); 
    } 
} 

は、あなたが説明した例外をスロー:あなたのクラスpublicを作る

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) 

は、問題を解決:

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://"); 
    } 
} 
関連する問題