0

I had problems with changing the font in a Jtextarea, I have 2 views(WidowsP,TypeJava), 2 controllers(Controler, Controler2) and one model. In this case my problem is in the jtextarea when I change the font the component sends an error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

In my principal view windowP I have the next components:

Jtextarea = areatexto;
JmenuItem = Jopcions;

My code for the model is:

public void fueltypes(JComboBox numbers,String[] Fonts, JComboBox list){
    for(int i =10;i<=30;i++){
        numbers.addItem(i);
    }
    for(String font : Fonts){
        list.addItem(font);
    }
}

public void changuefont(JTextArea area,String Fonts, int Size ){
    Font f = new Font(Fonts,Font.PLAIN,Size);
    area.setFont(f);
}

On the controller2 I have this code because in the form I only have 3 items jcombobox1 = The names fonts,jcombobox2 = Size and the only button part I'm changing the code is:

private TypeJava type;
private Window view;
private Modelo model;
private String Fonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

public controlador2(TypeJava b,Modelo m){
    this.type = b;
    this.model = m;
    this.type.JMod.addActionListener(this);
    this.type.jComboBox1.addActionListener(this);
    model.fueltypes(type.jComboBox2,Fonts,type.jComboBox1);
}

@Override
public void actionPerformed(ActionEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  if(e.getSource()== this.type.JMod){
   model.changuefont(view.areaTexto,
           type.jComboBox1.getSelectedItem().toString(),
           Integer.parseInt(type.jComboBox2.getSelectedItem().toString()));

  }

}
Draken
  • 3,134
  • 13
  • 34
  • 54
  • 1
    All `NullPointerExceptions` have the same root cause -- some object is null. Assuming that the exception is in the `area.setFont(f);` line, then the first place to start is to check if `area` is null before making the call. Work backwards in the call stack. However, from the small amount of code provided, I don't see where the `view` variable is ever set to anything. It is not set in the constructor (unlike `type`). – KevinO Oct 02 '18 at 17:08
  • Possible duplicate of [Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException Error](https://stackoverflow.com/questions/21228284/exception-in-thread-awt-eventqueue-0-java-lang-nullpointerexception-error) – Draken Oct 02 '18 at 17:17

0 Answers0