0

我有一个快速的问题。如何通过将参数传递给构造函数来创建新实例?由于 GraphicObject 可以是扩展它的任何类,我需要以某种方式知道它是哪个类,在这里我必须使用扩展它的类,所以我正在尝试反射,但到目前为止只有空异常。

    GraphicObject decor = null;

    try {
        decor = decorator.getClass().getDeclaredConstructor(GraphicObject.class).newInstance(mainGroup);
    } catch (Exception e) {
        System.out.println(e);
    }

应该反映的类如下所示:

public class DecoratorLeft extends GraphicObjectDecorator {

// jei bus klaida, tai bus del to, kad neuzsaugom per konstruktoriu paduodamos reiksmes i global variable
GraphicObject decoratedGraphic = null;

public DecoratorLeft(GraphicObject decoratedGraphic) {
    super(decoratedGraphic);
    this.decoratedGraphic = decoratedGraphic;
}

@Override
public void draw(Graphics g, AlignStrategy align) {
    alignLeft();
    decoratedGraphic.draw(g, align);

    //drawAlignedLeft(g, decoratedGraphic, align);        
}

}

这是错误消息:

java.lang.NullPointerException
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at objprog1.Whiteboard.paintComponent(Whiteboard.java:157)

我再次使用 printstactrace 和 runtimeexception 修改了 try/catch 语句:

    java.lang.NullPointerException
    at objprog1.Whiteboard.paintComponent(Whiteboard.java:179)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JSplitPane.paintChildren(JSplitPane.java:1047)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    at javax.swing.JComponent.paint(JComponent.java:1040)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.awt.Container.paint(Container.java:1967)
    at java.awt.Window.paint(Window.java:3867)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: java.lang.NullPointerException
    at objprog1.Whiteboard.paintComponent(Whiteboard.java:182)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JSplitPane.paintChildren(JSplitPane.java:1047)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paint(JComponent.java:1063)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:585)
    at javax.swing.JComponent.paintChildren(JComponent.java:887)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1206)
    at javax.swing.JComponent.paint(JComponent.java:1040)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115)
    at java.awt.Container.paint(Container.java:1967)
    at java.awt.Window.paint(Window.java:3867)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677)
    at javax.swing.RepaintManager.access$700(RepaintManager.java:59)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.lang.NullPointerException
    at objprog1.Whiteboard.paintComponent(Whiteboard.java:179)
    ... 39 more
4

1 回答 1

0

我认为你可以通过创建类似的东西来更简单地做到这一点:

protected GraphicObject getNewInstance ( GraphicObject decoratedGraphic ){ ... }

在你的GraphicObject类里面,所以每个继承它的类都会有这个方法,你可以直接从对象中调用它。

于 2012-10-03T01:02:56.883 回答