sir i got the reason why its null

in the action performed event i previously put the values of reflames and reflam to ""

but after rectifying it i got an error with insufficient memory space..

it i got an error

What was printed out?

the error is ::


Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap spa
ce
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at GameFlames.calculateInFlames(GameFlames.java:119)
at GameFlames.actionPerformed(GameFlames.java:168)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.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.lang.StringBuilder.append(Unknown Source)
at GameFlames.calculateInFlames(GameFlames.java:119)

In the calculateInFlames method at line 119 you are calling append.
What value values are being appended to what value?

this is the code sir

char calculateInFlames(int length)
   {
	 msg.setText("");
	 
	 reflam=reflam.append(reflam);
	 
	 reflames = reflam.toString();
	 for(int i=0;i<2*length;i++)
	 {
		System.out.println(reflames); 
		reflames=reflames+reflames;   //119 th line
	 }
	   int iterations=0;
	  int nextcharloc=0;
	   while(iterations<6)
	 {
		   
	    char removechar=reflames.charAt(length-1+nextcharloc);	
	    
	    char nextchar = reflames.charAt(length+nextcharloc);
	   
	    reflames= deleteAllOccurences(removechar);
         nextcharloc=reflames.indexOf(nextchar);
         iterations++;
         
	 }
			 
	  
		 return reflames.charAt(0);
   }

How long do you expect the reflames String to get to be?
Add a println to show its length as it goes around the loop.
How many times does it go around the loop?

sir i am getting correct code for this if length is small
and the number of flames to be appended is dynamic sir
it depends on the value of length
if its large the number should be more..


0 FlamesFlames
1 FlamesFlamesFlamesFlames
2 FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlames
3 FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlame
sFlamesFlamesFlames

for code

for(int i=0;i<2*length;i++)
	 {
		System.out.println(i+"  "+reflames); 
		reflames=reflames+reflames;   //119 th line
	 }

if its large the number should be more..

At some point it will be too much and you will get the error:
java.lang.OutOfMemoryError: Java heap space

Why are you building such a long String?

i have tried another code for having this last step
but they didnt work
so i tried this one

i mean SUPPOSE THE LENGTH WHICH COMES AS A PARAMETER BE 10

then i must be able to delete a character that counts to 10 in
"flamesflamesflamesflamesflamesflames"

here the character is 'm'
so i will be removing the character 'm' in the whole string..

later according to my code before deleting the letter i am taking the character next to it.. which is 'e' in this case..
and after deleting the letter 'm' i am searching for 'e' in the obtained string.
from this 'e' character again the next search goes on..

sir i got the total problem of the code..
but i need answer for this..
how to delete a character from a string??

String deleteAllOccurences(char remove)
	{
		String removechar=remove+"";
		reflames.replaceAll(""+removechar, "");
		System.out.println("in this method     "+reflames+"  "+removechar);
		return reflames;
	}

this is the output i am getting..

i understood that the string reflames is not being changed

3

0 FlamesFlames
1 FlamesFlamesFlamesFlames
2 FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlames
in this method FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesF
lamesFlamesFlamesFlamesFlamesFlames a
FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFl
amesFlamesFlames m a


in this method FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesF
lamesFlamesFlamesFlamesFlamesFlames s
FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFl
amesFlamesFlames F s


in this method FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesF
lamesFlamesFlamesFlamesFlamesFlames a
FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFl
amesFlamesFlames m a


in this method FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesF
lamesFlamesFlamesFlamesFlamesFlames s
FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFl
amesFlamesFlames F s


in this method FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesF
lamesFlamesFlamesFlamesFlamesFlames a
FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFl
amesFlamesFlames m a


in this method FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesF
lamesFlamesFlamesFlamesFlamesFlames s
FlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFlamesFl
amesFlamesFlames F s


help me please

This demonstrates a usage of replaceAll:

System.out.println("XaXbXc".replaceAll("X", ""));  // prints> abc

or in two statements:

String res = "XaXbXc".replaceAll("X", "");
System.out.println("res=" + res);    // res=abc

The program what has been written is showing error. Check n rewrite. Anyhow good idea

and finally i got another problem with the whole code

after executing the whole code i am getting a problem with the output
all the controls put onto the frame are not being displayed immediately after execution
i have to resize the frame so that it will be displaying all the controls..
how can i avoid this
please help me
and thanks in advance

all the controls put onto the frame are not being displayed immediately after execution

Do you initially set the size for the frame?
Or how is the size set?

setSize for JFrame must be calculated by Layout Manager, then each of JComponetns returns JComponetns.getPreferredSize(...), JFrame raised getScreenSize by call pack(), in another cases windows freeze, flickering during any action(s) with Container, action as resize, add/remove/replace/swich JCompoents....

sir i have got the desired output totally
the only problem is that the controls are not being displayed immediately after execution.
after i resize it i am getting the output

problem is that the controls are not being displayed immediately after execution.

Can you give more details? What is displayed on the window when the program starts execution before you touch it?

sometimes i am getting jst a label and a textbox
sometimes nothing
sometimes except buttons all are getting created...
getting different controls of the frame at different times

i am not understanding how to use getPrefferedSize() and setPreferredSize().
And
suppose i am having two JPanels and now
how can i set an image as background to a specific panel
please help me
thanks in advance

i set the size of the frame by using setSize();

how to delete a character from a string??

See the String class for methods to use.
Did I post an example of how to do this earlier?
Do a find for replaceAll for the example at the top of this page.

thanks a lot

and the final problem is that the when the button click event fires and if the textfields are empty even though there is a code for checking it, its giving an error

its giving an error

What is the error? You need to post its full text.

this is the code i used...

{
		
		if(ae.getSource()==calculate)
		{
			//msg.setText(""); 
		name_1=(first.getText()).toLowerCase();
		 name1=new StringBuffer(name_1);
		 name_2=second.getText().toLowerCase();
		 name2=new StringBuffer(name_2);
                   
                int len1,len2;
                   len1=name1.length();
                      len2= name2.length();
                      if(!((name1==name2))&&(len1>0)&&(len2>0))
                        {
                              
			 length= compare(name1,name2);
			 //System.out.println("\n\n\n "+length+"\n\n\n");
			msg.setText(length+"hai");
			char finalresult=calculateInFlames(length); 
		//msg.setText(finalresult+"");
		      Font f1=new Font("Monotype Corsiva", Font.BOLD,20); 
		      output.setForeground(Color.magenta);
		      output1.setForeground(Color.magenta);
		      output2.setForeground(Color.magenta);
		      output3.setForeground(Color.magenta);
		      output4.setForeground(Color.magenta);
		      rel.setForeground(Color.magenta);
		      
		      output.setFont(f1);
		      output1.setFont(f1);
		      output2.setFont(f1);
		      output3.setFont(f1);
		      output4.setFont(f1);
		      rel.setFont(f1);
		      name_1="\' "+name_1+" \"";
		      name_2="\' "+name_2+" \"";
		       switch(finalresult)
		       {
		       case 'f':  
		    	   j2.setVisible(true);
		    	   output.setText(" Relation between ");
		    	         output1.setText(name_1);
		    	         output2.setText("and ");
		    	         output3.setText(name_2);
		    	         output4.setText("is ");
		    	         rel.setText("FRIENDS");
		    	          break;
		    	          
		       case 'l':
		    	   j2.setVisible(true);
		    	   output.setText("relation between ");
		    	   output1.setText(name_1);
	    	         output2.setText("and");
	    	         output3.setText(name_2);
	    	         output4.setText("is");
		    	   
	    	          rel.setText("LOVE");
	    	          break;
		       case 'a': 
		    	   j2.setVisible(true);
		    	   output.setText("relation between ");
		    	   output1.setText(name_1);
	    	         output2.setText("and");
	    	         output3.setText(name_2);
	    	         output4.setText("is");
	    	          rel.setText("AFFAIR");
	    	          break;
		       case 'm':  
		    	   j2.setVisible(true);
		    	   output.setText("relation between ");
		    	   output1.setText(name_1);
	    	         output2.setText("and");
	    	         output3.setText(name_2);
	    	         output4.setText("is");
	    	          rel.setText("MARRIAGE");
	    	          break;
		       case 'e':  
		    	   j2.setVisible(true);
 		    	   output.setText("relation between ");
 		    	   output1.setText(name_1);
 	    	         output2.setText("and");
 	    	         output3.setText(name_2);
	    	         output4.setText("is");
 	    	          rel.setText("ENEMIES");
 	    	          break;
 		       case 's':  
 		    	   j2.setVisible(true);
 		    	   output.setText("relation between ");
 		    	   output1.setText(name_1);
 	    	         output2.setText("and");
 	    	         output3.setText(name_2);
	    	         output4.setText("is");
 	    	          rel.setText("SISTER");
 	    	          break;
 	    	
 		    	   
 		       
 		       
 		       
 		       }
 		 //msg.setText(reflames);
 		 }                    
                             
                        
                         

		 
	     if(name1==name2)
			 {
				System.out.println("different names to be entered");
                              msg.setForeground(Color.BLUE);
				Font f=new Font("arial",Font.BOLD,16);
				msg.setFont(f);
				 msg.setText("enter different names");
				
			 }
			 
	                
               }

i tried my best to solve the problem to solve it but i am not able to do it

i got this error...


Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException
: String index out of range: -1
at java.lang.String.charAt(Unknown Source)
at GameFlames.calculateInFlames(GameFlames.java:188)
at GameFlames.actionPerformed(GameFlames.java:222)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Sour
ce)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.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)


thanks in advance

StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(Unknown Source)
at GameFlames.calculateInFlames(GameFlames.java:188)

Look at your code at line 188. The index for charAt has an invalid value of -1.
Check how that index is being set and correct the logic so that you do not use an invalid index.

actually
if i provide the same name it should not go to that function

shall i post the whole code??

Did you find the call to the charAt() method?
Did you see how the value of the index can be -1?
You need to add a test in your code to be sure that the index is valid before using it.

for (int i = 0;i < nme.length() ;i++ )
{
n[i] =  nme.charAt(i);
}
for (int j=0;j < lnme.length() ;j++ )
{
l[j] =  lnme.charAt(j);
}

for (int i = 0;i < nme.length() ;i++ )
{
for (int j=0;j < lnme.length() ;j++ ){
if(n[i] == l[j]){
n[i] = ' ';
l[j]=' ';
}
}
}

int C = 0;
for (int i = 0;i < nme.length() ;i++ )
{
if(n[i] != ' '  ){
C ++;
}
}
for (int j=0;j < lnme.length() ;j++ )
{
if(l[j] != ' '  ){
C ++;
}
}

Recently i seen a simple program

<URL SNIPPED>

Please don't post on a one year old thread. Start your own new thread.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.