• 締切済み

エラー

java初心者です ある図形を描くプログラムなのですが★★のところで「Exception in thread "main" java.lang.NullPointerException」というエラーが出てまったく何も描きません。 コンストラクタなどがまだよく理解できていません。 それも含めてどなたか解説/アドバイス/模範回答などしていただけないでしょうか? public class ExOrbit{ public static void main(String[] args){ BodyOrbit orbit = new BodyOrbit(); BodyOrbit S = new BodyOrbit(); S.init(); /*orbit.init(170, 170, 10, 190);*/ while(true){   orbit.setD1(); ★ orbit.step();★ if(orbit.setD2() >= orbit.max0){ orbit.init(170, 170, 10); } } } } public class BodyOrbit{ public int x0, y0, dist0, max0; private int dist; public Turtle t; public BodyOrbit(){ x0=170; y0=170; dist0=10; max0=190; } public void init(){ this.x0 = x0; this.y0 = y0; this.dist0 = dist0; this.max0 = max0; this.dist = dist0; this.t = new Turtle(); this.t.move(this.x0, this.y0); this.t.penDown(); } public void init(int x0, int y0, int dist0){ this.dist = dist0; this.t.move(this.x0, this.y0); } public void setD1(){ this.dist+=10; } public void step(){ ★this.t.go(this.dist);★ this.t.rotate(120); } public int setD2(){ return this.dist; } }

  • Java
  • 回答数2
  • ありがとう数1

みんなの回答

  • OKwebb
  • ベストアンサー率44% (92/208)
回答No.2

orbit の t が new されていないからじゃないだろうか。

346mouse
質問者

補足

initの中のthis.t=new Turtle(); だけじゃダメということですか?

回答No.1

アドバイス以前にコンパイルできません。

346mouse
質問者

補足

すいません なぜコンパイルができないかわからないので質問しました…

関連するQ&A

  • Swing の実装でどうしてもエラーになります。

    初心者ですみませ。 次のリストがどうしてコンパイルを通っても実行時にエラーになってしまいます。どなたか、判る方原因を教えてください。 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; abstract class Figure { protected int x,y,width,height; protected Color color; public Figure(int x,int y,int w,int h,Color c) { this.x = x; this.y = y; width = w; height = h; color = c; } public void setSize(int w,int h) { width = w; height = h; } public void setLocation(int x,int y) { this.x = x; this.y = y; } abstract public void reshape(int x1,int y1,int x2,int y2); abstract public void paint(Graphics g); } class RectangleFigure extends Figure { public RectangleFigure(int x,int y,int w,int h,Color c) { super(x,y,w,h,c); } public void reshape(int x1,int y1,int x2,int y2) { int newx = Math.min(x1,x2); int newy = Math.min(y1,y2); int neww = Math.abs(x1 - x2); int newh = Math.abs(y1 - y2); setLocation(newx,newy); setSize(neww,newh); } public void paint(Graphics g) { g.setColor(color); g.drawRect(x,y,width,height); } } class DrawApplication { protected Vector figures; protected Figure drawingFigure; protected Color currentColor; protected DrawPanel drawPanel; public DrawApplication() { figures = new Vector(); drawingFigure = null; currentColor = Color.red; } public void setDrawPanel(DrawPanel c) { drawPanel = c; } public int getNumberOfFigures() { return figures.size(); } public Figure getFigure(int index) { return (Figure)figures.elementAt(index); } public void createFigure(int x,int y) { Figure f = new RectangleFigure(x,y,0,0,currentColor); figures.addElement(f); drawingFigure = f; drawPanel.repaint(); } public void reshapeFigure(int x1,int y1,int x2,int y2) { if (drawingFigure != null) { drawingFigure.reshape(x1,y1,x2,y2); drawPanel.repaint(); } } } class DrawPanel extends JPanel { protected DrawApplication drawApplication; public DrawPanel(DrawApplication app) { setBackground(Color.white); drawApplication = app; } public void paintComponent(Graphics g) { super.paintComponent(g); // //[すべてのFigureをpaintする] // Figure f = new RectangleFigure(0,0,0,0,drawApplication.currentColor); for(int i=0;i<drawApplication.getNumberOfFigures();i++){ f = drawApplication.getFigure(i); f.paint(g); } } } class DrawMouseListener implements MouseListener,MouseMotionListener { protected DrawApplication drawApplication; protected int dragStartX,dragStartY; public DrawMouseListener(DrawApplication a) { drawApplication = a; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { dragStartX = e.getX(); dragStartY = e.getY(); drawApplication.createFigure(dragStartX,dragStartY); } public void mouseReleased(MouseEvent e) { drawApplication.reshapeFigure(dragStartX,dragStartY,e.getX(),e.getY()); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { drawApplication.reshapeFigure(dragStartX,dragStartY,e.getX(),e.getY()); } public void mouseMoved(MouseEvent e) { } } class DrawMain { public static void main(String argv[]) { JFrame f = new JFrame("Draw"); // //[DrawApplicationとDrawPanelとDrawMouseListenerを作って組み立てる] // DrawApplication app = new DrawApplication(); JPanel c =new DrawPanel(app); c.addMouseListener(new DrawMouseListener(app)); c.addMouseMotionListener(new DrawMouseListener(app)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(c,BorderLayout.CENTER); f.setSize(400,300); f.setVisible(true); } }

    • ベストアンサー
    • Java
  • 線の太さを変える コードのエラの意味が分かりません

     java勉強中の初心者です、宜しくお願いします。  下のようなコードを書きましたが、  >DrawPanel dp = new DrawPanel( X1 , Y1 , X2 , Y2 ) ;  >g2.setStroke( の二か所でエラーが出ています。 (エラーの内容はよく理解できません。) 一体どこが間違っているのでしょうか宜しくお願いします。 ================================================================= public class drawLine extends JApplet { int X1 = 20 , Y1 = 20 , X2 = 150 , Y2 = 150 ; public void init() { DrawPanel dp = new DrawPanel( X1 , Y1 , X2 , Y2 ) ; // dp.setSize( 30 , 200 ) ; // dp.setBackground( Color.cyan ) ; this.add( dp ) ; this.setBounds( 10 , 10 , 400 , 400 ) ; this.setBackground( Color.cyan ) ; this.setVisible( true ) ; } } //======================================================= class DrawPanel extends JPanel { Float currentWidth = 20.0f ; int x1 , y1 , x2 , y2 ; public DrawPanel( int x1 , int y1 , int x2 , int y2 ) { this.x1 = x1 ; this.y1 = y1 ; this.x2 = x2 ; this.y2 = y2 ; Graphics2D g2 = (Graphics2D)this.getGraphics() ; g2.setStroke ( new BasicStroke ( currentWidth , BasicStroke.CAP_ROUND , BasicStroke.JOIN_MITER ) ) ; g2.setColor( Color.red ) ; g2.drawLine( x1 , y1 , x2 , y2 ) ; } } ==============================================================================

  • 「初心者です」-Xlint: deprecation

    import java.awt.*; import java.applet.*; public class ani_ball extends Applet implements Runnable { Thread th_mvball = null; int X,Y; double x1,y1; double x2,y2; double dx,dy; Graphics g; public void init() { this.setBackground(new Color(150,245,255)); this.X = 250; this.Y = 250; this.setSize(X,Y); this.x1 = this.x2 = this.X/2; this.y1 = this.y2 = this.Y/2; this.dx = 3; this.dy = 2; } public void paint(Graphics g) { g.setColor(Color.red); g.fillOval((int)(this.x2-3),(int)(this.y2-3),6,6); this.x1 = this.x2; this.y1 = this.y2; } public void start() { if(th_mvball == null) { th_mvball = new Thread(this); th_mvball.start(); } } public void run() { while(true) { try { this.move(); this.repaint(); Thread.sleep(10); } catch(InterruptedException e ) { this.stop(); } } } public void move() { if( y2 > Y ) { y2 = 2*Y - y1 - dy; dy = -dy; } else if ( y2 < 0) { y2 = -y1 - dy; dy = -dy; } else if ( x2 > X) { x2 = 2*X - x1 -dx; dx = -dx; } else if ( x2 < 0) { x2 = -x1 - dx; dx = -dx; } if( x2 < 0 ) { x2 = -x2 ; dx = -dx; } else if ( y2 < 0) { y2 = -y2; dy = -dy; } else if ( x2 > X) { x2 = 2*X - x2; dx = -dx; } else if ( y2 > Y) { y2 = 2*Y - y2; dy = -dy; } x2 = x1 + dx; y2 = y1 + dy; } public void stop() { if(th_mvball!=null) { th_mvball stop(); th_mvball=null; } } } ↑のプログラムをコンパイルすると、「-Xlint: deprecation オプションを指定して再コンパイルしてください」とエラーが出ます。 エラーの対処法、またはプログラムの訂正すべき箇所を教えてください。 よろしくお願いします。

    • ベストアンサー
    • Java
  • コンパイルエラー

    以下のようなソースファイルをコンパイルしたらコンパイルに失敗してしまいました。エラーは『Cls1はabstractで宣言する必要があります。show(int)をCls1で定義しません』と出ます。これはどういうことでしょうか。 interface Int1 { int x=100; void Show(int y); } class Cls1 implements Int1 { public void show(int x) { System.out.println(x); } } class Test12 { public static void main(String args[]) { Cls1 c; c = new Cls1(); c.show(200); } }

    • ベストアンサー
    • Java
  • メソッド

    public class Point{ public int x; public int y; public void swap(int s){ int x = s * y; y = s * x; this.x = x; } public void swap2(int s){ int x = s * y; y = s * this.x; this.x = x; } public static void main(String[] args){ Point pt = new Point(); pt.x = 3; pt.y = 4; pt.swap(2); System.out.println(pt.x + " " + pt.y); pt.swap2(3); System.out.println(pt.x + " " + pt.y); } } このプログラムでswapメソッドとswap2メソッドってどう違うんですか?

    • ベストアンサー
    • Java
  • コンパイルされない理由

    下のプログラミングをコンパイルするとエラーになるのですがどうしてですか?Javaの仕様だからといえばそれまでなのですが… また、どこを直したらよいのか教えてください。 class Point{ int x; int y; static void setPosition(int x,int y){ this.x = x; this.y = y; } }

  • NoSuchMethodErrorが解決できません。

    実行時エラーNoSuchMethodErrorが出て困っています。 どこを修正すればいいのでしょうか? class A implements Runnable{ int x; int y; public void run(){ for(int i = 0;i < 100;i++){ x++; y++; System.out.println("x="+x+"y="+y); } } } class B{ public static void main(String args[]){ new Thread(new A()).start(); new Thread(new A()).start(); } }

    • ベストアンサー
    • Java
  • javaについての質問です。

    javaについての質問です。 public class MyTurtleFrame extends TurtleFrame { Turtle[] turtleList = new Turtle[10]; /*ここの箇所をArraylistをつかって書き換える*/ int numTurtles = 0;/*ここの箇所をArraylistをつかって書き換える*/ public MyTurtleFrame() { this(400, 400); } public MyTurtleFrame(int width, int height) { super(width, height); JMenu turtles = new JMenu("Tuttle", true); this.frame.getJMenuBar().add(turtles); JMenuItem add = new JMenuItem("Add"); JMenuItem del = new JMenuItem("Delete"); add.addActionListener(this); del.addActionListener(this); add.setActionCommand("addTurtle"); del.setActionCommand("delTurtle"); turtles.add(add); turtles.add(del); this.frame.setVisible(true); } public void actionPerformed(ActionEvent e) { super.actionPerformed(e); if (e.getActionCommand().equals("addTurtle")) { this.addTurtle(); } if (e.getActionCommand().equals("delTurtle")) { this.delTurtle(); } this.repaint(); } protected void addTurtle() { int x = (int) (Math.random() * this.frame.getContentPane().getWidth()); int y = (int) (Math.random() * this.frame.getContentPane().getHeight()); try{ turtleList[numTurtles]=new Turtle(x, y, 0); this.add(turtleList[numTurtles]); numTurtles++; System.out.println(numTurtles); }catch(Exception a){ System.out.println("例外が発生しました");} } protected void delTurtle(){ try{ this.remove(turtleList[numTurtles-1]); numTurtles--; }catch(Exception a){ System.out.println("例外が発生しました"); } } public static void main(String[] args) { MyTurtleFrame f=new MyTurtleFrame(); } } 文中のコメントが書いてある箇所をArraylistをつかって書き換えて,それに応じてaddTurtleとdelTurtleも適切に書き換える問題なのですが解決の糸口がどうしてもわかりません。 解答を導くヒントをご教授いただけるかたよろしくお願いします。

    • ベストアンサー
    • Java
  • java

    次のコード中の括弧内で下に示す11通りの各コードを実行した場合の実行画面を正確に答えよ。 class A{ public void func1(){System.out.println("A1");} public void func2(){System.out.println("A2");} } class B extends A{ public void func1(){System.out.println("B");} } class C{ public int x=0, y=1; } class D extends C{ public int x=2; public void func1(int x){System.out.println(x);} public void func2(int x){System.out.println(this.x);} public void func3(int x){System.out.println(super.x);} public void func4(int x){System.out.println(this.y);} public void func5(int x){System.out.println(super.y);} } class E{ public void func1(int n){ try{ System.out.println("E1"); int[] ary=new int[n]; System.out.println("E2"); }catch(NegativeArraySizeException e){ System.out.println("E3"); }finally{ System.out.println("E4"); } } } (1) A a=new A(); a.func1(); (2) A a=new B(); a.func1(); (3) B b=new B(); b.func1(); (4) B b=new B(); b.func2(); (5) D d=new D(); d.func1(3); (6) D d=new D(); d.func2(3); (7) D d=new D(); d.func3(3); (8) D d=new D(); d.func4(3); (9) D d=new D(); d.func5(3); (10) E e=new E(); e.func1(5); (11) E e=new E(); e.func1(-2); (1) A1 (2)B (3)B (4) A2 (5) 3 (6) 2 (7) 0 (8) 1 (9) 1 (10) E1E2E4(11) E1E3E4 と答えになるんですがなぜこうなるのかわかりません。教えてください

    • ベストアンサー
    • Java
  • C#のインクリメント演算子のオーバーロード(前置きと後置き)

    インクリメント演算子をオーバーロードして、後置きインクリメントの場合に戻り値が演算前の結果を返すようにする方法はありませんか? class Sample {  public int x;  public int y;  public Sample(int x, int y)  {   this.x = x;   this.y = y;  }  public override string ToString()  {   return base.ToString() + " - x = " + x + ", y = " + y;  }  public static Sample operator ++()  {   x++; y++;   return this;  } } class EntryPoint {  public static void Main()  {   Sample sample = new Sample(1, 2);   // 「Sample - x = 2, y = 3」で、期待通り   Console.WriteLine(++sample);   // 「Sample - x = 3, y = 4」で、期待したのはインクリメントされる前の値である「「Sample - x = 2, y = 3」   Console.WriteLine(sample++);  } }

専門家に質問してみよう