Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 37

Thread: I have a problem with reading 5 strings from an AWT dialog window.

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Cool I have a problem with reading 5 strings from an AWT dialog window.

    I have studied Java programming basics at university....i like it much and started to learn more about it. I have began with AWT and learned most about it ...but only doing small programs with 2-3 components. Now i try to make a complex program to train myself with what i learned. And i have some problems with a step of it. I want a dialog window with 5 textfields and each should read each a string when i click Ok and use them for an object of a class. But looks like i cannot read those strings... i get lots of errors and when to see which kind of exception i have ...i got java.lang.NullPointerException. I did before a smaller program with a dialog window which reads a single String and that one has not problem. I really have no clue why it does not work. If any of you can give me an idea how to fix it or if i have to learnd about Swing and Appelt to be able to this...please tell me ( at least me not waste too much time with something i do not know). I paste here the code of the class with the dialog frame.
    package introducereIntrebari;
     
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
     
    public class DialogIntroducereIntrebare extends Dialog implements ActionListener{
     
    	Button ok;
    	TextField ta1;
    	TextField tf1,tf2,tf3,tf4,tf5,tf6;
    	String itr;
    	String r1;
    	String r2;
    	String r3;
    	String r4;
    	String r5;
    	String r6;
     
     
    	public DialogIntroducereIntrebare(Frame parinte, String titlu, boolean modala) {
    		super( parinte,  titlu, modala);
     
    		this.addWindowListener(new WindowAdapter ()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				dispose();
    			}
    		});
     
     
    		setLayout (new GridLayout(6,1));
     
    		Panel intrebare = new Panel();
    		Label lab1 = new Label ("Intrebarea:",Label.LEFT);
    		lab1.setBackground(Color.lightGray);
    		TextField ta1 = new TextField("",30);
    		intrebare.add(lab1, BorderLayout.NORTH);
    		intrebare.add(ta1, BorderLayout.SOUTH);
     
    		Panel raspuns1 = new Panel ();
    		Label lab2 = new Label ("a) ", Label.LEFT);
    		lab2.setBackground(Color.lightGray);
    		TextField tf1 = new TextField("", 20);
    		raspuns1.add(lab2, BorderLayout.EAST);
    		raspuns1.add(tf1, BorderLayout.WEST);
     
    		Panel raspuns2 = new Panel ();
    		Label lab3 = new Label ("b) ", Label.LEFT);
    		lab3.setBackground(Color.lightGray);
    		TextField tf2 = new TextField("", 20);
    		raspuns2.add(lab3, BorderLayout.EAST);
    		raspuns2.add(tf2, BorderLayout.WEST);
     
    		Panel raspuns3 = new Panel ();
    		Label lab4 = new Label ("c) ", Label.LEFT);
    		lab4.setBackground(Color.lightGray);
    		TextField tf3 = new TextField("", 20);
    		raspuns3.add(lab4, BorderLayout.EAST);
    		raspuns3.add(tf3, BorderLayout.WEST);
     
    		Panel raspuns4 = new Panel ();
    		Label lab5 = new Label ("d) ", Label.LEFT);
    		lab5.setBackground(Color.lightGray);
    		TextField tf4 = new TextField("", 20);
    		raspuns4.add(lab5, BorderLayout.EAST);
    		raspuns4.add(tf4, BorderLayout.WEST);
     
    		Panel raspuns5 = new Panel ();
    		Label lab6 = new Label ("e) ", Label.LEFT);
    		lab6.setBackground(Color.lightGray);
    		TextField tf5 = new TextField("", 20);
    		raspuns5.add(lab6, BorderLayout.EAST);
    		raspuns5.add(tf5, BorderLayout.WEST);
     
    		Panel raspuns6 = new Panel ();
    		Label lab7 = new Label ("Raspunsul Corect ", Label.LEFT);
    		lab7.setBackground(Color.lightGray);
    		TextField tf6 = new TextField("", 20);
    		tf6.setBackground(Color.RED);
    		ok = new Button("OK");
    		raspuns6.add(lab7, BorderLayout.EAST);
    		raspuns6.add(tf6, BorderLayout.WEST);
    		raspuns6.add(ok, BorderLayout.SOUTH);
     
    		add(intrebare);
    		add(raspuns1);
    		add(raspuns2);
    		add(raspuns3);
    		add(raspuns4);
    		add(raspuns5);
    		add(raspuns6);
     
    		ta1.addActionListener(this);
    		tf1.addActionListener(this);
    		tf2.addActionListener(this);
    		tf3.addActionListener(this);
    		tf4.addActionListener(this);
    		tf5.addActionListener(this);
    		tf6.addActionListener(this);
    		ok.addActionListener(this);
     
    		setBackground(Color.lightGray);
     
    		setSize (600,800);
    		show();
     
    	}
     
     
     
    	public void actionPerformed(ActionEvent e)
    	{
     
    		if (e.getActionCommand()=="OK" )
    		{
     
     
     
     
    			try
    			{
    				System.out.println(ta1.getText());
    				System.out.println(tf1.getText());
    				System.out.println(tf2.getText());
    				System.out.println(tf3.getText());
    				System.out.println(tf4.getText());
    				System.out.println(tf5.getText());
    				System.out.println(tf6.getText());
    			}
     
     
    			catch (Exception e1)
    			{
    				System.out.println(e1);
    			}
     
    			dispose();
    		}
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    i get lots of errors
    Please copy and paste here the full text of your error messages.

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    There is no main method. We cannot attempt to compile and run your application..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Well i have a class with main ....I have 4 longer classes at all but this is the class where i get the problems.
    If i do not put there the exception thing i get

    Exception occurred during event dispatching:
    java.lang.NullPointerException
    	at introducereIntrebari.DialogIntroducereIntrebare.actionPerformed(DialogIntroducereIntrebare.java:123)
    	at java.awt.Button.processActionEvent(Unknown Source)
    	at java.awt.Button.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$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.pumpEventsForFilter(Unknown Source)
    	at java.awt.Dialog$1.run(Unknown Source)
    	at java.awt.Dialog$3.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.awt.Dialog.show(Unknown Source)
    	at introducereIntrebari.DialogIntroducereIntrebare.<init>(DialogIntroducereIntrebare.java:107)
    	at introducereIntrebari.Fereastra.actionPerformed(Fereastra.java:42)
    	at java.awt.Button.processActionEvent(Unknown Source)
    	at java.awt.Button.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    	at java.awt.EventQueue.access$000(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.awt.EventQueue$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.awt.EventQueue$2.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$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)


    and this when i click the OK button of this dialog. The class with the frame seems to have no problems...but i will past that aswell

    package introducereIntrebari;
     
    import java.awt.*;
    import java.awt.event.*;
     
    public class Fereastra extends Frame implements ActionListener {
    	Button adauga;
    	Button sterge;
     
    	Fereastra (String titlu)
    	{
    		super(titlu);
     
    		this.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				System.exit(0);
    			}
    		});
    		setSize(200,300);
    		Panel butoane = new Panel();
    		adauga = new Button ("Adauga");
    		sterge = new Button ("Sterge");
    		butoane.add(adauga, BorderLayout.WEST);
    		butoane.add(sterge, BorderLayout.EAST);
     
    		setLayout(new BorderLayout());
    		this.add(butoane, BorderLayout.SOUTH);
     
    		adauga.addActionListener(this);
    		sterge.addActionListener(this);
     
     
    	}
     
     
    	public void actionPerformed(ActionEvent e) {
     
    		if (e.getActionCommand()=="Adauga")
    		{
    			new DialogIntroducereIntrebare (this, "Introdu Intrebarea", true );
    		}
     
    	}
     
    }


    and the main class is like this

    package introducereIntrebari;
     
    import java.util.*;
     
    public class IntroduceIntrebari {
     
    	public static void main(String[] args) {
    		Fereastra f = new Fereastra ("Incercare");
    		f.show();
     
    	}
     
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    java.lang.NullPointerException
    at introducereIntrebari.DialogIntroducereIntrebare.ac tionPerformed(DialogIntroducereIntrebare.java:123)
    Look at line 123 in the mentioned source code. What variable at that line is null? Then look to see why it is null.
    If you can't see which variable is null, add a println before statement 123 to print out the values of all the variables used in line 123.

    Check that you are not shadowing class variables by defining local versions of them in a method. When the method exits the local versions of the variables go away and the class level variables are still null.
    Last edited by Norm; July 21st, 2011 at 08:05 AM.

  6. #6
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Hmm ... well i have replaced

    System.out.println(ta1.getText());
    				System.out.println(tf1.getText());
    				System.out.println(tf2.getText());
    				System.out.println(tf3.getText());
    				System.out.println(tf4.getText());
    				System.out.println(tf5.getText());
    				System.out.println(tf6.getText());

    with

    			itr= ta1.getText();
    				r1=tf1.getText();
    				r2=tf2.getText();
    				r3=tf3.getText();
    				r4=tf4.getText();
    				r5=tf5.getText();
    				r6=tf6.getText();

    also tried like this..
    // this at the beginning of the code
    	private String itr;
    	private String r1;
    	private String r2;
    	private String r3;
    	private String r4;
    	private String r5;
    	private String r6;
     
    // and this at the actionListener
     
    itr= itr+ta1.getText();
    				r1=r1+tf1.getText();
    				r2=r2+tf2.getText();
    				r3=r3+tf3.getText();
    				r4=r4+tf4.getText();
    				r5=r5+tf5.getText();
    				r6=r6+tf6.getText();

    And still get same error. Well, here... r1,r2,r3,r4,r5,r6 are Strings that are null at the beginning but those should change to the strings that are written on ta1,tf1,tf2,tf3,tf4,tf5,tf6 TextFields. Maybe it is wrong if those text fields are empty..but i still get that even if i write something on them.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    And still get same error
    What variable is null?
    You must find that so you can fix it.
    So look at the line where the exception occurs and see what variable is there.

  8. #8
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    i think the problem is at tf1.getText() i do not understand why but seems it does not want to read the text from there. i have tried to delete itr= itr+ta1.getText(); and see if the others work ...but i get same on next lines.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Where is the variable: tf1 given a value?

    BTW See my post #5

  10. The Following User Says Thank You to Norm For This Useful Post:

    piulitza (July 21st, 2011)

  11. #10
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Oh yeah ...finally understood. Before i written the values and dimensions of the textfields inside the constructor method. But now i have written that out of the constructor, making them public variables in that class.... and now it works much better. Thanks much.

  12. #11
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    I have one more question...... how can i read an int from textfield? I see i have only method to read string

  13. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    See the API for Integer, in particular the parseInt method

  14. #13
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    I have one more question (hope i do not become annoying). How can i check if a TextField is empty?

    i have tried all these :

     
    int i = 0
    if (tf1.getText().equals(null))
    			{
    				i++;
    			}

    or

     
    int i = 0
    if (tf1.getText()==null)
    			{
    				i++;
    			}


    or

     
    int i = 0
    if (tf1.getText()=="")
    			{
    				i++;
    			}

    and even if there is nothing written on the textfield...i still get i = 0 after the "if" (here, tf1 is the textfield). Why? I searched little on google but not found something to help

  15. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    What does the getText() method return?
    What methods does that class have that you can use to test if ...

  16. #15
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    getText() returns a String....after thinking little more i have tried isEmpty() method for the string and seems that Wroks !

  17. #16
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    I need some more advice.As i do not know anything about database yet... I try to save all the Strings from these TextFields into an object so i need access them into another dialog. I have tried to serialize them but seems i cannot make it work like that,....i am not sure if i serialize the objects each one replaces the one that is written before into the file or writes one more object there. I have also tried to serialize an array of objects but do not manage to read that array from another class. Is serialization the best option or there any other way i can do that ?

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    to save all the Strings from these TextFields into an object
    Why save into an object? If the data to be saved is String, you could write them to a file.

    Otherwise make a small program to show your problems with serialization and post it here with comments and your questions.

  19. #18
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Yeah ..i think i would try to use to write the Strings directly.I have a smaller program i did longer time ago that serialize an array of object. And that works very well and i understood basically how it works. Mostly i wanted to list the serialized objects into another dialog and to have the option to delete some of them...but i do not see any method to delete a serialized object from the array. Well...i would try to write each string to a file with BufferedWriter and read each 6 and assign them to another object when i want to use them. Basicaly i want to make a program that saves a question, 5 possible answers for it and the correct answer...and after be able read them and make kind of grid test. Seems i will have to make the thing with dialog on which you able to see the questions and able to manage them after i learn more.

  20. #19
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    hmm looks like BufferedWriter and other writing methods overwrites the text in the file each time i introduce the Strings.. and i want to gather them all. I will leave it for a while until i learn Swing ...maybe i will find there ways to do this easier.

  21. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    methods overwrites the text in the file each time
    There is a parameter to some of the file I/O classes constructors that allow you to append your output to an existing file.

  22. The Following User Says Thank You to Norm For This Useful Post:

    piulitza (July 23rd, 2011)

  23. #21
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    I think i need little more help.In the dialog with introducing the questions i have managed to write the string to a file using BufferedReader (written 2 of them in the file), and i have done aswell for each string the spaces turn into "_" so i ensure that when i read each 7 string i read exactly those i need to introduce into another object to contain the strings . Next, i have tried to place in the class with Question's setters and getters a static method to read the questions from the file and build a String with them. It loos like this:
    package introducereIntrebari;
     
    import java.io.*;
    import java.util.*;
     
    public class Intrebare  {
    	String intrebare;
    	String raspuns1;
    	String raspuns2;
    	String raspuns3;
    	String raspuns4;
    	String raspuns5;
     
    	String raspunsCorect;
    	String raspunsul;
     
    	Intrebare (String intr, String r1, String r2, String r3, String r4, String r5, String rul)
    	{
    		intr = intrebare;
    		r1 = raspuns1;
    		r2 = raspuns2;
    		r3 = raspuns3;
    		r4 = raspuns4;
    		r5 = raspuns5;
    		rul = raspunsCorect;
    	}
     
    public static Intrebare[] ListaCompletaIntrebari (String url,int j)
    	{
    		Intrebare[] intr = new Intrebare[j];
     
    		try
    		{
    			Scanner in = new Scanner (new File(url));	
     
    			for (int i=0;in.hasNext();i++)
    			{
    				intr[i].setIntrebare(in.next());
    				intr[i].setRaspuns1(in.next());
    				intr[i].setRaspuns2(in.next());
    				intr[i].setRaspuns3(in.next());
    				intr[i].setRaspuns4(in.next());
    				intr[i].setRaspuns5(in.next());
    				intr[i].setRaspunsCorect(in.next());
     
    			}
     
     
    		}
     
    		catch (Exception e)
    		{
    			System.out.println(e);
    		}
    		return intr;
     
    	}


    and into a class with main i have tried

    package testarea;
     
    import java.io.*;
    import java.util.Scanner;
     
    import introducereIntrebari.Intrebare;
     
    public class MainClass {
    	public static void main(String[] args) {
     
     
    		Intrebare[] intr = new Intrebare[2];
     
    		intr = Intrebare.ListaCompletaIntrebari("C:\\Users\\npiulitza\\Desktop\\persoane\\intrebari.txt", intr.length);
     
     
    		for (int i =0; i<intr.length; i++)
    		{
     
    		System.out.println(intr[i].getIntrebare());
    		System.out.println(intr[i].getRaspuns1());
    		System.out.println(intr[i].getRaspuns2());
    		System.out.println(intr[i].getRaspuns3());
    		System.out.println(intr[i].getRaspuns4());
    		System.out.println(intr[i].getRaspuns5());
    		System.out.println(intr[i].getRaspunsCorect());
     
    		}
    	}
     
     
    }

    and i get

    java.lang.NullPointerException
    Exception in thread "main" java.lang.NullPointerException
    	at testarea.MainClass.main(MainClass.java:20)

    So, the Strings are not read properly (when i tried to read each one without introducing them into an array worked but i need to make an array with them).
    Any clue where i do wrong ?

  24. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    NullPointerException
    at testarea.MainClass.main(MainClass.java:20)
    What variable on line 20 is null? Back track in your code to see why it doesn't have a value and change the code to give it a value.

    In the ListaCompletaIntrebari method you should check that you read all the data that is expected to be read into the array. I assume that j is the number of expected items.
    Last edited by Norm; July 24th, 2011 at 10:38 AM.

  25. #23
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    Yeah, there j is the number of expected items .. and on line 20

    intr[i].getIntrebare()
    is the null. i have also changed little the for from the class Intrebare from
    for (int i=0;in.hasNext();i++)
    to
    for (int i=0;i<j&&in.hasNext();i++)
    to ensure that i do not read more items than the the length of the array. But still not manage to understand why intr[i].getIntrebare() is null.

  26. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    why intr[i].getIntrebare() is null.
    Where do assign a value to the element i in the intr[] array?
    What is the value of i with you get the exception?
    Have that many elements been put into the array?
    Add a println in the code that fills the array to show the values for the indexes where/when it puts values into the array.

  27. #25
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: I have a problem with reading 5 strings from an AWT dialog window.

    I have replaced the static method from the from the first class to

    public static Intrebare[] ListaCompletaIntrebari (int j,String url)
    	{
     
    		Intrebare intr = new Intrebare();
    		Intrebare[] intrArray = new Intrebare[j];
    		try
    		{
    			Scanner in = new Scanner (new File(url));	
     
    			for (int i=0;in.hasNext()&&i<j;i++)
    			{
     
     
    				intr.setIntrebare(in.next());
    				intr.setRaspuns1(in.next());
    				intr.setRaspuns2(in.next());
    				intr.setRaspuns3(in.next());
    				intr.setRaspuns4(in.next());
    				intr.setRaspuns5(in.next());
    				intr.setRaspunsCorect(in.next());
     
    				intrArray[i] = intr;
     
    			}
     
     
    		}
     
    		catch (Exception e)
    		{
    			System.out.println(e);
    		}
     
    		return intrArray;
     
    	}

    Seems to work somehow... i no longer get null when i println the objects elements in the main ....only problem...seems like both objects are the same ...even if i read 2 different sets of string in the file.

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] Problem in File Reading...
    By Mr.777 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 20th, 2011, 07:24 AM
  2. reading content of the text file, splitting it into strings
    By Dodo in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: January 6th, 2011, 07:57 PM
  3. Reading contents of another window
    By jimmys in forum Java Theory & Questions
    Replies: 8
    Last Post: October 4th, 2010, 11:40 PM
  4. Facing problem with open modeless dialog from modal window
    By Divya in forum Java Theory & Questions
    Replies: 1
    Last Post: May 14th, 2009, 03:18 AM
  5. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM