我正在尝试创建一个方法,使用一对2D坐标(其字段名称以“X”结尾)的任何“X”组件的整数值获取封闭实例中的任何字段。
public int getX(String object) throws NullPointerException{
String objectX = object + "X";
Component c = null;
try{
Integer x = new Integer(object);
x = c.getX();
if(object != null){
return x;
}
}catch (NullPointerException e){} // if the referenced object does not exist.
System.err.println("The object "+object+"does not exist or the name was spelled incorrectly.");
return (Integer)null;
}
但我在编译器中得到的只是:
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "append"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.<init>(Unknown Source)
at PlazmaBurst2.Body.getX(Body.java:118)
at PlazmaBurst2.Body.append(Body.java:79)
at PlazmaBurst2.Body.<init>(Body.java:53)
at PlazmaBurst2.PB2Main$DrawingCanvas.paintComponent(PB2Main.java:40)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at javax.swing.RepaintManager$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
该方法将使用如下:
Image img = setImage("myImage.png");
int imgX = getX(img);// use of getX(String)
我在这里做错了什么?
答案 0 :(得分:2)
您似乎认为将对象传递给String参数作为参数传入对象的标识符。
换句话说,如果你这样做:
public static void printObject(String ostring) {
System.out.println(ostring);
}
Object object = new Object();
printObject(object);
然后你认为这将打印“对象”,实际上你的IDE会发出警告,因为它是不可编译的,或者(可能)默默地更改代码以调用对象的toString
方法。
你想要做到这一点的方式可能是通过一些黑客反射来获得任何对象的字段“...... X”但不要这样做。它被黑了。
相反,如果你真的想要像这样做“通用”,你可以使用你的类实现的接口。接口适用于此类事物。
// not to be confused with java.awt.Point
public interface Point {
public Point getPoint();
public int getX();
public int getY();
}
任何类都可以实现接口,方法返回的内容取决于该实现。您可以在任何实现getX
的类上调用Point
,由该类决定采取适当的操作。接口在实现类型方面也是不可知的。
// redundant but shows the point
public static int getXFromPoint(Point pt) {
return pt.getX();
}
class PointImplementation implements Point {
private Component c = somewhereElse.getAComponent();
@Override public int getX() {
return c.getX();
}
@Override public int getY() {
return c.getY();
}
@Override public Point getPoint() {
return c.getLocation();
}
}
System.out.println(getXFromPoint(new PointImplementation()));
不确定您实际想要从Image(java.awt.Image?)返回什么。为什么不调用适当的方法?
如果你真的想以你想要的方式(从字符串中访问字段?)这样做,那么Java反射教程将涵盖这一点:http://docs.oracle.com/javase/tutorial/reflect/member/field.html
答案 1 :(得分:0)
也许问题是因为对象!它是一个包含Integer的字符串。尝试运行捕获NumberFormatException
的代码 public int getX(String object) throws NullPointerException{
String objectX = object + "X";
Component c = null;
try{
try{
Integer x = new Integer(object);
} catch(NumberFormatException ex){
System.out.println("You were trying to convert <" + object "> to Integer but it's impossible");
}
// The above line c was initiated to null so I delete this line for thest x = c.getX();
x = (int)(Math.random() * 100) ;
if(object != null){
return x;
}
}catch (NullPointerException e){} // if the referenced object does not exist.
System.err.println("The object "+object+"does not exist or the name was spelled incorrectly.");
return (Integer)null;
}
答案 2 :(得分:0)
您似乎将该对象的X值作为字段存储在该类中,并且您正在尝试创建一个查询该对象的X值的方法。
问题是,当一个对象的identifier实际需要一个字符串(字段的名称?)时,它会将reflection传递给getX(String)
。如果要获取字段的值,可以使用HashMap按名称获取字段的值:
public int getX(String object) {
try {
return Plasmaburst2.Body.class.getDeclaredField(object + "X");
} catch(NoSuchFieldException | SecurityException e) {
return 0;
}
}
但是,出于性能原因,您可以考虑使用{{3}}而不是反射,或者只创建自己的类来存储对象的坐标。