• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to solve the error "non-static method can not be referenced from static context"

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I m creating n Text Editor using JEditorPane().
I want to change the color of the text as i type ne new thing in editorpane.
For that i have used the folloeing code:--
set=new SimpleAttributeSet();
StyleConstants.setForeground(set, Color.RED);
StyleConstants.setUnderline(set, true);StyledEditorKit.StyledTextAction.setCharacterAttributes(editor,set,true);

but it's showing me following error:-

non-static method setCharacterAttributes(javax.swing.JEditorPane,javax.swing.text.AttributeSet,boolean) cannot be referenced from a static context.

What does it means n how to solve it.

Plz help me.
Thanks in Advance.
Regards,
Danish Shaikh.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't believe you have given us enough of your code to point the exact problem for your situation.

However, if you have one method that is not static, you cannot call it from another method which is static.



If you made myMethod() static in the above example it would be legal.

[ November 22, 2004: Message edited by: Michael Parmeley ]
[ November 22, 2004: Message edited by: Michael Parmeley ]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I found this old post and it sounds like the same problem I am having. I created two classes that extend JFrame and when I try to run a method or update a variable from one class to another, I get an error that says "non-static method can not be referenced from static context"

I am pretty new to Java, so this is probably something basic I am not understanding. Below is the class file that I am getting the error in.

Hope someone can tell me what I am missing, thanks
---------------------------------------------------

package main;
import orders.OrdersUI;

public class Menu extends javax.swing.JFrame {
public Menu() {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Menu().setVisible(true);
}
});
}

private void navOrdersButtonActionPerformed
(java.awt.event.ActionEvent evt){
OrdersUI.setVisible(true);
//the above line of code causes the error
}

---------------------------------------------------
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by danish shaikh:
I m creating n Text Editor using JEditorPane().
I want to change the color of the text as i type ne new thing in editorpane.
For that i have used the folloeing code:--
set=new SimpleAttributeSet();
StyleConstants.setForeground(set, Color.RED);
StyleConstants.setUnderline(set, true);
StyledEditorKit.StyledTextAction.setCharacterAttributes(editor,set,true);



Well you've got a couple of problems here. The one the compiler is complaining about is that setCharacterAttributes() is not a static method in StyledEditorKit.StyledTextAction. I guess that's why this thread was moved to the beginner forum.

In addition, StyledEditorKit.StyledTextAction is an abstract class and it presumes that the JEditorPane's EditorKit is a StyledEditorKit. If your EditorKit isn't a StyledEditorKit (or a subclass, such as HTMLEditorKit) then StyledEditorKit.StyledTextAction.setCharacterAttributes() will throw a IllegalArgumentException.

On the other hand, if you know that it is a StyledEditorKit, then you don't need to use StyledTextAction. You could call getInputAttributes().addAttributes(set) directly on your StyledEditorKit.


btw, if you would use a JTextPane instead of a JEditorPane things might be easier. Not only would that guarantee that you were using a StyledEditorKit, but JTextPane defines setCharacterAttributes() and getInputAttributes() methods that you can use directly without messing with EditorKits.



Is there a reason you need to use JEditorPane instead of JTextPane?
[ January 26, 2007: Message edited by: Brian Cole ]
 
Gloria McCarthy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, thanks for your reply, but you replied to the question from Nov, 2004. I guess I should of started a new post.

My question is. I created two classes that extend JFrame and when I try to run a method or update a variable from one class to another, I get an error that says "non-static method can not be referenced from static context" Or in other words, now do you create two classes and set a variable or run another method without getting this error?

I am pretty new to Java, so this is probably something basic I am not understanding. Below is the class file that I am getting the error in.

Hope someone can tell me what I am missing, thanks
---------------------------------------------------

package main;
import orders.OrdersUI;

public class Menu extends javax.swing.JFrame {
public Menu() {
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Menu().setVisible(true);
}
});
}

private void navOrdersButtonActionPerformed
(java.awt.event.ActionEvent evt){
OrdersUI.setVisible(true);
//the above line of code causes the error
}
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, just going to take a stab at this (I'm relatively new also).

Try this:


Also, is the above method intended to be included in the Menu() class?(Just checking as there is one brace missing at the end if it is indeed supposed to be included.)

BTW, I entered your code into my editor, and added the aforementioned brace; and I did indeed get a graphical frame (which I know you did as well). So if my suggestion is incorrect and you want to share the OrdersUI file I would be happy to look further into this from my end (provided you did not find a resolution elsewhere).
[ January 27, 2007: Message edited by: Ryan Giomi ]
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static variables and methods exists whether an object is created or not, and are shared across all instances. Non-static variables only exist once an object has been created, and then only for that specific object.

You need to create an OrdersUI object, then you can set that objects property to true.

Think about it this way... without creating the object, WHAT exactly is going to be visible?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic