Results 1 to 3 of 3

Thread: Help with Lists

  1. #1
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default 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)

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,650
    Blog Entries
    7
    Rep Power
    21

    Default 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>();
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default 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 ++;
    		}
    to the paint method, but it still doesn't draw anything. I'm assuming I need a repaint in my main loop but my main loop is in my static main method, and that doesn't follow OOP. The only solution I can think of is to make a new class for my main loop, or stuff it into the textwall class. Is this an efficient solution?
    Last edited by Darkzombies; 05-04-2013 at 10:21 PM.

Similar Threads

  1. help with linked lists
    By akusa in forum New To Java
    Replies: 1
    Last Post: 04-15-2013, 04:34 AM
  2. Want to know something about lists.
    By orushw in forum New To Java
    Replies: 2
    Last Post: 05-08-2012, 09:41 PM
  3. A little trouble with Lists....
    By kovalensue in forum New To Java
    Replies: 1
    Last Post: 12-04-2010, 09:56 PM
  4. Help with lists
    By datreta in forum New To Java
    Replies: 6
    Last Post: 10-29-2010, 11:33 AM
  5. Linked Lists
    By vendetta in forum New To Java
    Replies: 6
    Last Post: 01-26-2010, 08:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •