A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 创造命运 中级黑马   /  2014-4-23 12:15  /  1109 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 创造命运 于 2014-4-24 13:43 编辑


请问这里为什么会报异常:未知属性“x”。我将"x"变为大写或是小写都会报异常。又会报不同的异常,如:x属性没有getter方法。
x属性是在ReflectPoint类里面定义的,我检查过,确信有这个变量,同时也有get跟set方法。
我又对了下代码跟张老师是否有出入,没问题。可是为什么我这里就是要报异常呢?还有其他哪些地方我没想到的吗?

评分

参与人数 1技术分 +1 收起 理由
ily521125 + 1

查看全部评分

6 个回复

倒序浏览
把 代码发上来 看看
回复 使用道具 举报
霍振鹏 发表于 2014-4-23 12:46
把 代码发上来 看看

package reflectDemo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

public class IntroSpectorTest {
        public static void main(String[] args) throws Exception {
                ReflectPoint pt1 = new ReflectPoint(3, 5);
                System.out.println(BeanUtils.getProperty(pt1, "X"));       //改行报异常 //.getClass().getName()
                //以字符串的形式对属性进行操作,因为在web开发过程中,浏览器对数据的操作都是以字符串的形式进行的
                BeanUtils.setProperty(pt1, "X", "9");   
                System.out.println(pt1.getX());  //这里将pt1类看做一个普通的类。       
                 //"123"是一个毫秒值,如果在ReflectPoint类中没有实例化Date,这里会报空指针异常。
                BeanUtils.setProperty(pt1, "birthday.time", "123");
                System.out.println(BeanUtils.getProperty(pt1, "birthday.time"));
                PropertyUtils.setProperty(pt1, "X", 9);    //该工具是以属性的真实类型对属性进行操作
                System.out.println(PropertyUtils.getProperty(pt1, "X"));
        }
}
//ReflectPoint类
package reflectDemo;
import java.util.Date;
class ReflectPoint {
        public int x = 3;
        public int y = 5;
        private Date birthday = new Date();
       
        public ReflectPoint(){}
        public ReflectPoint(int x, int y)
        {
                this.x = x;
                this.y = y;
        }
        public int getX() {
                return x;
        }
        public void setX(int x) {
                this.x = x;
        }
        public int getY() {
                return y;
        }
        public void setY(int y) {
                this.y = y;
        }
        public Date getBirthday() {
                return birthday;
        }
        public void setBirthday(Date birthday) {
                this.birthday = birthday;
        }
        public String toString() {
                return "ReflectPoint [x=" + x + ", y=" + y + "]";
        }
        public int hashCode() {
                final int prime = 31;
                int result = 1;
                result = prime * result + x;
                result = prime * result + y;
                return result;
        }
        public boolean equals(Object obj) {
                if (this == obj)
                        return true;
                if (obj == null)
                        return false;
                if (getClass() != obj.getClass())
                        return false;
                ReflectPoint other = (ReflectPoint) obj;
                if (x != other.x)
                        return false;
                if (y != other.y)
                        return false;
                return true;
        }       
}

评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

回复 使用道具 举报
把public加上,至于为什么要加,我也没弄明白,求讨论?
不过你的问题还是解决了

aaaaa.png (9.12 KB, 下载次数: 6)

aaaaa.png
回复 使用道具 举报
霍振鹏 发表于 2014-4-23 18:48
把public加上,至于为什么要加,我也没弄明白,求讨论?
不过你的问题还是解决了
...

谢啦。我刚试了试,还是不行。这下连编译都不通过了。
异常如此:ReflectPoint cannot be resolved to a type.
我把所有源代码给你看吧。

reflectDemo.rar

3.05 KB, 下载次数: 28

回复 使用道具 举报
本帖最后由 霍振鹏 于 2014-4-24 10:47 编辑
创造命运 发表于 2014-4-24 07:17
谢啦。我刚试了试,还是不行。这下连编译都不通过了。
异常如此:ReflectPoint cannot be resolved to a  ...

把所有的x都换成小写
运行结果:

abc.png (6.83 KB, 下载次数: 23)

abc.png

reflectDemo.zip

3.46 KB, 下载次数: 23

回复 使用道具 举报
霍振鹏 发表于 2014-4-24 10:45
把所有的x都换成小写
运行结果:

谢啦,好了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马