黑马程序员技术交流社区

标题: 为什么有异常呢 [打印本页]

作者: 创造命运    时间: 2014-4-23 12:15
标题: 为什么有异常呢
本帖最后由 创造命运 于 2014-4-24 13:43 编辑


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

作者: 霍振鹏    时间: 2014-4-23 12:46
把 代码发上来 看看
作者: 创造命运    时间: 2014-4-23 17:42
霍振鹏 发表于 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;
        }       
}


作者: 霍振鹏    时间: 2014-4-23 18:48
把public加上,至于为什么要加,我也没弄明白,求讨论?
不过你的问题还是解决了

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

aaaaa.png

作者: 创造命运    时间: 2014-4-24 07:17
霍振鹏 发表于 2014-4-23 18:48
把public加上,至于为什么要加,我也没弄明白,求讨论?
不过你的问题还是解决了
...

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

reflectDemo.rar

3.05 KB, 下载次数: 28


作者: 霍振鹏    时间: 2014-4-24 10:45
本帖最后由 霍振鹏 于 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 13:43
霍振鹏 发表于 2014-4-24 10:45
把所有的x都换成小写
运行结果:

谢啦,好了。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2