Results 1 to 3 of 3
Thread: Help with Lists
- 05-04-2013, 08:27 AM #1
Senior Member
- Join Date
- Nov 2012
- Posts
- 105
- Rep Power
- 0
Help with Lists
Basically what I want is a program that holds information in neat lines as it is added. Scrolls when it gets to the bottom. I am trying to create it because it can be used for many things, like a text adventure. While I'm at that, I'd also like to know how I can have an input using graphics and not the output. It won't compile or print anything onto the screen, in addition to that there are parts that just don't seem very optimized.
Java Code:import java.applet.Applet; import java.awt.*; import javax.swing.JFrame; public class Main extends Applet{ public static boolean gameOver = false; public static int gameState = 0; public static TextWall tw = new TextWall(); public void paint(Graphics g){ } public static void main(String[] args){ JFrame frame = new JFrame(); Main a = new Main(); frame.getContentPane().add(a, BorderLayout.CENTER); frame.setSize(new Dimension(1360,768)); frame.setVisible(true); tw.addInfo("Test"); tw.repaint(); //Main Loop while(!gameOver){ if(gameState == 0){ //Main Menu } else if(gameState == 1){ } else if(!(gameState == 1 || gameState == 0)){ //Example tw.addInfo("Please enter a valid number"); tw.repaint(); //Example } } } }
Java Code:import java.awt.Graphics; import java.util.List; public class TextWall { private List<String> info; private Graphics g; private int lastY = 0; public TextWall(){ } public void addInfo(String s){ info.add(s); } public void removeInfo(int i){ if(info.contains(i)){ info.remove(i); }else{ System.err.println("String was not found."); } } public List<String> getInfo(){ return info; } public void repaint(){ for(int x = 0; x > info.size(); x++){ g.drawString(info.get(x), 100, lastY * 100); lastY ++; } } }
Exception in thread "main" java.lang.NullPointerException
at TextWall.addInfo(TextWall.java:16)
at Main.main(Main.java:23)
- 05-04-2013, 11:57 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Re: Help with Lists
You're not creating a List<String> in that class for your 'info' reference variable; the constructor is a nice place to do so, i.e.
Java Code:public TextWall(){ info= new ArrayList<String>(); }
JosBuild a wall around Donald Trump; I'll pay for it.
- 05-04-2013, 09:50 PM #3
Senior Member
- Join Date
- Nov 2012
- Posts
- 105
- Rep Power
- 0
Re: Help with Lists
That took care of the error, but it still doesn't print anything to the screen, so I moved
Java Code:for(int x = 0; x >= tw.getInfo().size(); x++){ g.drawString(tw.getInfo().get(x), 100, lastY * 100); lastY ++; }
Last edited by Darkzombies; 05-04-2013 at 11:21 PM.
Similar Threads
-
help with linked lists
By akusa in forum New To JavaReplies: 1Last Post: 04-15-2013, 05:34 AM -
Want to know something about lists.
By orushw in forum New To JavaReplies: 2Last Post: 05-08-2012, 10:41 PM -
A little trouble with Lists....
By kovalensue in forum New To JavaReplies: 1Last Post: 12-04-2010, 10:56 PM -
Help with lists
By datreta in forum New To JavaReplies: 6Last Post: 10-29-2010, 12:33 PM -
Linked Lists
By vendetta in forum New To JavaReplies: 6Last Post: 01-26-2010, 09:23 AM
Bookmarks