0

UN proclaimed 2015 as the International Year of Light and Light-based Technologies (IYL 2015). We have developed the Turkey official web site and also devolop three interactive applet that help young generation to understand some basic fundementals of electromagnetic waves.

The applets can run when i try to run from IDEs. But each of them gives "nullpointerexception" error when try to run at browser by html codes. I need help to solve the error.

Doppler applet is an eclipse project, the others are Intellij projects. I give the source code blow.

HTML Code sample that i use to add applets to web site (actually, the code is orginally generated by netbeans):

<APPLET codebase="classes" code="src/Doppler.class" width=350 height=200></APPLET>

Refraction-Reflection Applet:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Refraction extends JApplet implements ActionListener {

int finPoint=200;
int upperLineFinx=finPoint;
int upperLineFiny=finPoint;
int bottomLineBegx=upperLineFinx;
int bottomLineBegy=upperLineFiny;
int lengthOfLines=150;
double teta1=50*Math.PI/180;
double n1=1;
double n2= 1;
double teta2= Math.asin((n1 / n2) * Math.sin(teta1)) ;
double temp;
static final int angle_min = 0;
static final int angle_max = 90;
static final int angle_rate = 15;
Vector model1=new Vector();
Vector model2=new Vector();
JSlider angle_slider;
JComboBox n1ComboBox,n2ComboBox;
JButton button1;
JTextField incidenceTF,refractionTF,n1TF,n2TF;
JLabel yan_kir_lab;
Graphics bufferGraphics;
Image offImg;
Dimension dim,dimSlider;

public Refraction(){
    model1.addElement(new Item(1,"Hava"));
    model1.addElement(new Item(1.309,"Buz"));
    model1.addElement(new Item(1.333,"Su"));
    model1.addElement(new Item(1.36,"Ethanol"));
    model1.addElement(new Item(1.47,"Zeytin Yağı"));
    model1.addElement(new Item(1.48,"Cam"));
    model1.addElement(new Item(2.42,"Elmas"));

    button1=new JButton("Değiştir");
    n1ComboBox=new JComboBox(model1);
    n2ComboBox=new JComboBox(model1);
}

public void init(){
    incidenceTF=new JTextField(String.valueOf(teta1/Math.PI*180));
    refractionTF=new JTextField(String.valueOf(teta2/Math.PI*180));
    n1TF=new JTextField(String.valueOf(n1));
    n2TF=new JTextField(String.valueOf(n2));
    yan_kir_lab=new JLabel(" Kırılma Açısı=");
    dim= new Dimension(600, 400);
    this.setSize(dim);
    setBackground(Color.white);
    setLayout(new BorderLayout());
    Panel p1=new Panel();
    Panel p2=new Panel();
    Panel p3=new Panel();
    p1.setBackground(new Color(0xEBE669));
    p1.setLayout(new GridLayout(10, 0));
    addLabel(" Aci", p1);
    addSlider(p1);
    addnCombobox(n1ComboBox, 1, p1);
    addnCombobox(n2ComboBox,2,p1);
    p1.add(new Label());
    p2.setBackground(new Color(0xEBE669));
    p2.setSize(new Dimension(200, 200));
    p2.setLayout(new GridLayout(2, 2));
    p2.add(new JLabel(" Gelme Açısı="));
    p2.add(incidenceTF);
    p2.add(yan_kir_lab);
    p2.add(refractionTF);
    p1.add(p2);
    p3.setBackground(new Color(0xEBE669));
    p3.setSize(new Dimension(200, 200));
    p3.setLayout(new GridLayout(2, 2));
    p3.add(new JLabel(" n1="));
    p3.add(n1TF);
    p3.add(new JLabel(" n2="));
    p3.add(n2TF);
    p1.add(p3);
    add(p1, BorderLayout.EAST);
    offImg=createImage(dim.width,dim.height);
    bufferGraphics=offImg.getGraphics();
    angle_slider.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            teta1=(((JSlider) e.getSource()).getValue())*Math.PI/180;
            temp=(n1 / n2) * Math.sin(teta1);
            //Math.asin((n1 / n2) * Math.sin(teta1 * Math.PI / 180.0))
            if(temp>=1){
                teta2 = teta1;
                yan_kir_lab.setText(" Yansıma Açısı=");
            }
            else{ teta2 = Math.asin(temp);
                  yan_kir_lab.setText(" Kırılma Açısı=");
            }
            String teta1tostring=String.valueOf(teta1/Math.PI*180);
            String teta2tostring=String.valueOf(teta2/Math.PI*180);
            if(teta1tostring.length()>5) teta1tostring=teta1tostring.substring(0,5);
            if (teta2tostring.length()>5) teta2tostring=teta2tostring.substring(0,5);
            incidenceTF.setText(teta1tostring);
            refractionTF.setText(teta2tostring);
            repaint();
        }
    });
    this.setVisible(true);

}
public void actionPerformed(ActionEvent e) {



    if(e.getSource()==n1ComboBox){
        Item item1 = (Item) n1ComboBox.getSelectedItem();
        n1 = item1.getValue();
        repaint();
    }
    else if (e.getSource()==n2ComboBox){
        Item item2=(Item) n2ComboBox.getSelectedItem();
        n2=item2.getValue();
        repaint();
    }

    repaint();
}

public void addLabel( String text ,Container cont){
    JLabel label=new JLabel();
    label.setText(text);
    cont.add(label);
}

public void addSlider(Container conteiner){
    angle_slider=new JSlider(JSlider.HORIZONTAL,angle_min,angle_max,angle_rate);
    dimSlider=new Dimension(200,40);
    angle_slider.setValue((int)(teta1*180/Math.PI));
    angle_slider.setBackground(new Color(0xEBE669));
    angle_slider.setMajorTickSpacing(15);
    angle_slider.setMinorTickSpacing(5);
    angle_slider.setPaintLabels(true);
    angle_slider.setPreferredSize(dimSlider);
    angle_slider.setAlignmentX(Component.RIGHT_ALIGNMENT);
    angle_slider.setVisible(true);
    conteiner.add(angle_slider);
}
public void addnCombobox(JComboBox cb, final int whichComboBox,Container cont){

    cb.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if(whichComboBox==1){
                Item item1 = (Item) n1ComboBox.getSelectedItem();
                n1 = item1.getValue();
                n1TF.setText(String.valueOf(n1));
                repaint();
            }
            else if (whichComboBox==2){
                Item item2=(Item) n2ComboBox.getSelectedItem();
                n2=item2.getValue();
                n2TF.setText(String.valueOf(n2));
                repaint();
            }
        }
    });
    cont.add(cb);
}

public void paint (Graphics g) {


    bufferGraphics.clearRect(0, 0, dim.width, dim.height);
    bufferGraphics.setColor(Color.white);

    bufferGraphics.setColor(new Color(0xB1EFEE));
    bufferGraphics.fillRect(0, 200, 400, 400);

    bufferGraphics.setColor(Color.red);
    bufferGraphics.drawLine(200,0,200,400);
    bufferGraphics.drawLine(0,200,400,200);

    bufferGraphics.setColor(Color.black);
    bufferGraphics.drawLine((int)(200-lengthOfLines*Math.sin(teta1)),(int)(200-lengthOfLines*Math.cos(teta1)),upperLineFinx,upperLineFiny);


    if(temp>=1){
        bufferGraphics.drawLine(bottomLineBegx,bottomLineBegy,(int)(200+lengthOfLines*Math.sin(teta2)),(int)(200-lengthOfLines*Math.cos(teta2)));
    }
    else {
        bufferGraphics.drawLine(bottomLineBegx, bottomLineBegy, (int) (200 + lengthOfLines * Math.sin(teta2)), (int) (200 + lengthOfLines * Math.cos(teta2)));
    }
    g.drawImage(offImg,0,0,this);
}
public void update(Graphics g)
{
    paint(g);
}




class Item
{
    private double value;
    private String description;

    public Item(double value, String description)
    {
        this.value = value;
        this.description = description;
    }

    public double getValue()
    {
        return value;
    }

    public String getDescription()
    {
        return description;
    }

    public String toString()
    {
        return description;
    }
}

}

Group and Phase Velocity Applet:

import javafx.scene.control.Slider;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BasicStroke;
import java.awt.Graphics2D;


public class GrupVeFazHizi extends JApplet implements ActionListener {

Timer timer= new Timer(30,this);
Graphics2D bufferGraphics;
Image offImg;
Dimension dim;
int[] xs;
int[] ys,ys2,ys3;
int pixelNumber=600;
int yBase=120;
double t;
int waveAmp=100;
private double k=0.3;
int phaseVel=20;
double groupVel=phaseVel*1.4;
double L=50;
boolean repeatCheck;
Dimension sliderDim,labelDim;
Border b = BorderFactory.createLineBorder( new Color(0x96937E), 1 );
JSlider groupVelSlider=new JSlider(JSlider.HORIZONTAL,0,100,1);
JSlider phaseVelSlider=new JSlider(JSlider.HORIZONTAL,0,100,1);
JSlider waveAmpSlider=new JSlider(JSlider.HORIZONTAL,0,150,1);
JSlider kSlider=new JSlider(JSlider.HORIZONTAL,0,10,1);
JSlider lSlider=new JSlider(JSlider.HORIZONTAL,0,100,1);


Label grupHiziLabel=new Label("            Grup Hızı");
public void init(){
    xs=new int[pixelNumber];
    ys=new int[pixelNumber];
    ys2=new int[pixelNumber];
    ys3=new int[pixelNumber];
    labelDim=new Dimension(50,50);
    grupHiziLabel.setPreferredSize(labelDim);
    prepareSliders();

    repeatCheck=false;
    t=0.01;
    dim=new Dimension(600,400);
    this.setSize(dim);
    setBackground(Color.white);
    offImg=createImage(dim.width,dim.height);
    bufferGraphics=(Graphics2D) offImg.getGraphics();
    setLayout(new BorderLayout());
    Panel p1= new Panel();
    p1.setSize(new Dimension(200,200));
    p1.setBackground(new Color(0xFF9C7C));
    p1.setLayout(new GridLayout(2, 5));
    //p1.add(new Label("Grup Hızı"));
    p1.add(grupHiziLabel);
    p1.add(new Label("             Faz Hızı"));
    p1.add(new Label("       Dalga Genliği"));
    p1.add(new Label("     Dalga Sayısı*10"));
    p1.add(new Label("        Dalga Boyu"));
    addSlider(groupVelSlider, p1);
    addSlider(phaseVelSlider, p1);
    addSlider(waveAmpSlider, p1);
    addSlider(kSlider, p1);
    addSlider(lSlider,p1);
    add(p1, BorderLayout.SOUTH);
    timer.setRepeats(true);
    timer.start();
    for(int i=0;i<pixelNumber;i++){
        xs[i]=i;
        ys[i]=yBase;
        ys2[i]=yBase;
        ys3[i]=yBase;
    }
}
public void paint (Graphics g){
    bufferGraphics.clearRect(0,0,dim.width,dim.height);
    bufferGraphics.setColor(Color.white);
    bufferGraphics.setStroke(new BasicStroke(1.0f));

    bufferGraphics.setColor(Color.black);
    bufferGraphics.drawPolyline(xs, ys, pixelNumber);
    bufferGraphics.setColor(Color.red);
    bufferGraphics.setStroke(new BasicStroke(2.0f));
    bufferGraphics.drawPolyline(xs,ys2,pixelNumber);
    bufferGraphics.drawPolyline(xs,ys3,pixelNumber);
    g.drawImage(offImg,0,0,this);
}
public void update(Graphics g)
{
    paint(g);
}
@Override
public void actionPerformed(ActionEvent e) {
    for(int i=0;i<pixelNumber;i++){
        ys[i]=yBase+(int) (waveAmp*(Math.exp(-Math.pow((((xs[i]+110)-groupVel*t)/L),2))* Math.cos(k*(xs[i]-phaseVel*t))));
        ys2[i]=yBase+(int)(waveAmp*Math.exp(-Math.pow((((xs[i]+110)-groupVel*t)/L),2)));
        ys3[i]=yBase+(int)(-waveAmp*Math.exp(-Math.pow((((xs[i]+110)-groupVel*t)/L),2)));
    }
    if(ys2[pixelNumber-1]>yBase+2){
        repeatCheck=true;
    }
    if(repeatCheck==true && ys2[pixelNumber-1]<=(yBase)){
        repeatCheck=false;
        t=0.01;
    }
    t=t+0.05;
    repaint();
}
public void addSlider(final JSlider slider,Container container){
    slider.setPreferredSize(new Dimension(100,20));
    slider.setAlignmentX(Component.LEFT_ALIGNMENT);
    slider.setBackground(new Color(0xF5FF9B));
    slider.setBorder(b);
    slider.setVisible(true);
    slider.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            if(slider==groupVelSlider)             groupVel=slider.getValue();
            else if (slider==phaseVelSlider)       phaseVel=slider.getValue();
            else if (slider==waveAmpSlider)        waveAmp=waveAmpSlider.getValue();
            else if (slider==kSlider)              k=kSlider.getValue()/10.0;
            else                                   L=lSlider.getValue();
            repaint();
        }
    });
    container.add(slider);
}
private void prepareSliders(){
    sliderDim=new Dimension(150,40);
    groupVelSlider.setPreferredSize(sliderDim);
    phaseVelSlider.setPreferredSize(sliderDim);
    waveAmpSlider.setPreferredSize(sliderDim);
    kSlider.setPreferredSize(sliderDim);
    lSlider.setPreferredSize(sliderDim);

    groupVelSlider.setValue((int) groupVel);
    phaseVelSlider.setValue(phaseVel);
    waveAmpSlider.setValue(waveAmp);
    kSlider.setValue((int) (k*10));
    lSlider.setValue((int) L);

    groupVelSlider.setMajorTickSpacing(50);
    phaseVelSlider.setMajorTickSpacing(50);
    waveAmpSlider.setMajorTickSpacing(75);
    kSlider.setMajorTickSpacing(5);
    lSlider.setMajorTickSpacing(50);

    groupVelSlider.setPaintLabels(true);
    phaseVelSlider.setPaintLabels(true);
    waveAmpSlider.setPaintLabels(true);
    kSlider.setPaintLabels(true);
    lSlider.setPaintLabels(true);
}
}

Doppler Effect Applet:

package Doppler;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.applet.AppletContext;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.awt.BasicStroke;
import java.awt.Graphics2D;



public class Doppler extends JApplet implements ActionListener {

Graphics2D bufferGraphics;
Image offImg;
Dimension dim;
Timer timer= new Timer(30,this);
int waveVelocity=3;
int waveNumber=1000;
int waveFrequency=30;
int frequencyCheck=0;
int maxFrequency=60;
Circle[] circles = new Circle[waveNumber];
AmbulanceObj amb=new AmbulanceObj();
Image ambulanceImage,buildings, ground;
AppletContext context;
int circleNumber=waveNumber;
private int sayac;
Label ambVelLabel,waveVelLabel,frequencyLabel;
JSlider ambVelSlider=new JSlider(0,5,1);
JSlider waveVelSlider=new JSlider(0,5,1);
JSlider frequencySlider=new JSlider(1,maxFrequency,1);
Border b = BorderFactory.createLineBorder( new Color(0x96937E), 1 );





public class Circle {
    int centx=0;
    int centy=0;
    int radiusx=0;
    int radiusy=0;
    int velocity=0;
    public Circle(int x,int y, int radx,int rady, int vel){
        this.centx=x;
        this.centy=y;
        this.radiusx=radx;
        this.radiusy=rady;
        this.velocity=vel;
    }
    public Circle(int x,int y) {
        this.centx = x;
        this.centy = y;
    }
}
public class AmbulanceObj{
    int ambX=0;
    int ambY=255;
    int ambWidth=50;
    int ambHeight=20;
    int ambVelocity=1;
}
public void addSlider(final JSlider slider,Container container){
    slider.setPreferredSize(new Dimension(100,20));
    slider.setAlignmentX(Component.LEFT_ALIGNMENT);
    slider.setBackground(new Color(0xF5FF9B));
    slider.setBorder(b);
    slider.setVisible(true);
    slider.addChangeListener(new ChangeListener() {
        @Override
        public void stateChanged(ChangeEvent e) {
            if(slider==ambVelSlider) amb.ambVelocity=ambVelSlider.getValue();
            else if(slider==waveVelSlider) waveVelocity=waveVelSlider.getValue();
            else if (slider==frequencySlider) waveFrequency=maxFrequency-frequencySlider.getValue()+1;
            repaint();
        }
    });
    container.add(slider);
}
public Image getImage(String name)
{
    URL imgUrl = getClass().getClassLoader().getResource("resources/"+name);
    ImageIcon icon = new ImageIcon(imgUrl);
    return icon.getImage();
}

public void init(){
    amb.ambX=-20;
    ambVelLabel=new Label("   Ambulans Hızı:");
    waveVelLabel=new Label("     Dalga Hızı:");
    frequencyLabel=new Label("      Frekans:");
    ambVelSlider.setValue(amb.ambVelocity);
    waveVelSlider.setValue(waveVelocity);
    frequencySlider.setValue(maxFrequency-waveFrequency+1);
    ambulanceImage= getImage("ambulance1.png");
    buildings= getImage("rsz_building3.png");
    ground= getImage("rsz_3ground.png");

    for (int i=0;i<circleNumber;i++){
        circles[i]=new Circle(amb.ambX+amb.ambWidth,amb.ambY);
        circles[i].velocity=0;
    }

    dim= new Dimension(600,400);
    this.setSize(dim);
    setBackground(new Color(0xD4FFFC));
    setLayout(new BorderLayout());
    Panel p1= new Panel();
    p1.setSize(new Dimension(600,20));
    p1.setBackground(new Color(0xEBE669));
    p1.setLayout(new GridLayout(0, 6));
    p1.add(ambVelLabel);
    addSlider(ambVelSlider,p1);
    p1.add(waveVelLabel);
    addSlider(waveVelSlider,p1);
    p1.add(frequencyLabel);
    addSlider(frequencySlider,p1);
    add(p1, BorderLayout.SOUTH);

    offImg=createImage(dim.width,dim.height);
    bufferGraphics=(Graphics2D) offImg.getGraphics();
    sayac=0;
    timer.setRepeats(true);
    timer.start();

}

public void paint( Graphics g){

    bufferGraphics.clearRect(0,0,dim.width,dim.height);
    bufferGraphics.drawImage(buildings,0,0,this);
    bufferGraphics.drawImage(ambulanceImage,amb.ambX,amb.ambY,this);
    bufferGraphics.setColor(new Color(0xBF3002));
    bufferGraphics.setStroke(new BasicStroke(2.0f));
    for(int a=0;a<waveNumber;a++) {
        bufferGraphics.drawOval(circles[a].centx - (circles[a].radiusx / 2), circles[a].centy - (circles[a].radiusy / 2), circles[a].radiusx, circles[a].radiusy);
    }
    bufferGraphics.setStroke(new BasicStroke(1.0f));
    bufferGraphics.drawImage(ground,0,300,this);
    g.drawImage(offImg,0,0,this);
}
public void update(Graphics g)
{
    paint(g);
}

@Override
public void actionPerformed(ActionEvent e) {
    amb.ambX+=amb.ambVelocity;
    if((frequencyCheck% (double) waveFrequency==0 && amb.ambX+amb.ambWidth<600) ){
        circles[sayac].centx=amb.ambX+amb.ambWidth;
        circles[sayac].centy=amb.ambY+10;
        circles[sayac].velocity=waveVelocity;
        sayac+=1;
    }


    else if(amb.ambX+amb.ambWidth>=600 || circles[waveNumber-1].velocity>0){
        amb.ambX=-20;
        for (int i=0;i<circleNumber;i++){
            circles[i]=new Circle(amb.ambX+amb.ambWidth,amb.ambY);
            circles[i].velocity=0;
        }
        sayac=0;
        frequencyCheck=0;
    }
    for(int a=0;a<waveNumber;a++){
        circles[a].radiusx+=circles[a].velocity;
        circles[a].radiusy+=circles[a].velocity;
    }
    frequencyCheck=frequencyCheck+1;
    repaint();
}
}
  • What have you tried to diagnose it? I assume you have tried all the obvious fixes like, reading the stack trace and checking that no variable used is `null`. – Peter Lawrey Feb 23 '15 at 11:49
  • You can't dump all the code here and expect someone to solve the problem, especially when where the error occurs is not marked and no stack trace is included. – npinti Feb 23 '15 at 11:49
  • I'm sorry if i could not clarify the error. i have tried to explain in sentence "The applets can run when i try to run from IDEs. But each of them gives "nullpointerexception" error when try to run at browser by html codes. I need help to solve the error." but clearly i am not successful. The problem is that the applets can be build and run perfectly by IDEs. But after i did every thing that i can reach about applet publish, they can not be run by browser. JVM gives "null pointer exception" when i try to run at browser. – Oğuz Tekin Feb 23 '15 at 14:08
  • i tried to move project to netbeans and could run perfectly. netbeans has a feature that automaticly generate an simple html code to run applets in browser. However, the same applet could not be run at this page. SO, i don't know that if problem is about codes or something else. That is why i did not mark the somewhere of code. The real reason of giving you to codes is that you may want to help to develop these scientific applet – Oğuz Tekin Feb 23 '15 at 14:13
  • You have to sign your applet with an official certificate if you want it to run in a webbrowser. So don't use the *.class but create a jar file and sign it... – Lonzak Feb 23 '15 at 15:51
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Feb 24 '15 at 02:22

0 Answers0