0

I'm new to the java language and I tried to make a program with the help of google, this is an applet program but it doesn't run even though there are no errors in eclipse. I am also confused with the cardLayout if it can be used in the applet, so I'll be grateful for the help.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

import java.io.*;
import javax.swing.*;

public class Proto extends Applet implements ActionListener {
    Label Store = new Label("WELCOME TO SCHOOL SUPPLIES STORE");
    double Price, Subtotal;
    String Code;
    String ITname;
    String quan;
    String choice;
    int qty;
    double Total;
    CardLayout card1;
    Container c, d;
    File writer;
    Button buttonBUY = new Button ("BUY");
    Button buttonCONFIRM = new Button("Confirm");
    Button buttonCONFIRM1 = new Button("Confirm your transaction?");
    Button back = new Button ("Back");
    Button close = new Button ("Close");
    Font StoreFont = new Font("Times New Roman", Font.BOLD, 15);


    Label flb1 = new Label ("Item");
    Label flb2 = new Label ("Quantity");
    Label flb3 = new Label ("Price");
    Label flb4 = new Label ("SUbtotal");
    Label lb1 = new Label("ITEM CODE  ");
    Label lb2 = new Label("  PRODUCT NAME  ");
    Label lb3 = new Label("  PRICE  ");
    Label lb4 = new Label ("  QUANTITY ");


    Label c1 = new Label ("HBW01");
    Label HBW01pn = new Label ("  HBW Ballpen  ");
    Label HBW01p = new Label ("  8.00PHP  ");

    Label c2 = new Label ("MNG02");
    Label MNG02pn = new Label ("  Mongol Pencil  ");
    Label MNG02p = new Label ("  6.00PHP  ");

    Label c3 = new Label ("MRK03");
    Label MRK03pn = new Label ("  Marker/Pentel  ");
    Label MRK03p = new Label ("  30.00PHP    ");

    Label c4 = new Label ("YLW04");
    Label YLW04pn = new Label ("  One Whole Yellow Pad  ");
    Label YLW04p = new Label ("  30.00PHP    ");

    Label c5 = new Label ("CRT05");
    Label CRT05pn = new Label ("  Cartolina  ");
    Label CRT05p = new Label ("  10.00PHP    ");

    Label c6 = new Label ("MNL06");
    Label MNL06pn = new Label ("  Manila Paper  ");
    Label MNL06p = new Label ("  6.00PHP    ");

    Label c7 = new Label ("EGL07");
    Label EGL07pn = new Label ("  Elmer's Glue  ");
    Label EGL07p = new Label ("  12.00PHP    ");

    Label c8 = new Label ("CPR08");
    Label CPR08pn = new Label ("  Colored Paper ");
    Label CPR08p = new Label ("  1.00PHP    ");

    Label c9 = new Label ("BPR09");
    Label BPR09pn = new Label ("  Bond Paper  ");
    Label BPR09p = new Label ("  1.00PHP    ");

    Label c10 = new Label ("OPR10");
    Label OPR10pn = new Label ("  Oslo Paper  ");
    Label OPR10p = new Label ("  1.00PHP    ");

    Label c11 = new Label ("GPR11");
    Label GPR11pn = new Label ("  Graphing Paper  ");
    Label GPR11p = new Label ("  1.00PHP    ");

    Label c12 = new Label ("RLR12");
    Label RLR12pn = new Label ("  Ruler  ");
    Label RLR12p = new Label ("  10.00PHP    ");

    Label c13 = new Label ("PRT13");
    Label PRT13pn = new Label ("  Protractor  ");
    Label PRT13p = new Label ("  10.00PHP    ");

    Label c14 = new Label ("ERS14");
    Label ERS14pn = new Label ("  Eraser  ");
    Label ERS14p = new Label ("  5.00PHP    ");

    Label c15 = new Label ("FLR15");
    Label FLR15pn = new Label ("  Notebook Filler  ");
    Label FLR15p = new Label ("  15.00PHP    ");

    Label c16 = new Label ("SCR16");
    Label SCR16pn = new Label ("  Scissor  ");
    Label SCR16p = new Label ("  10.00PHP    ");
    TextArea TA = new TextArea (10, 10);
    TextArea RP = new TextArea (10, 80);
    TextArea CH = new TextArea (10, 10);
    TextArea TOT = new TextArea (10, 10);


public void init() {
    back.addActionListener(this);
    buttonBUY.addActionListener(this);
    buttonCONFIRM.addActionListener(this);
    buttonCONFIRM1.addActionListener(this);
    close.addActionListener(this);
    card1 =new CardLayout(800,600);
    c.setLayout(card1);
    c.add(Store);
    c.add(lb1);c.add(lb2);c.add(lb3);c.add(lb4);
    c.add(c1);c.add(HBW01pn);c.add(HBW01p);
    c.add(c2);c.add(MNG02pn);c.add(MNG02p);
    c.add(c3);c.add(MRK03pn);c.add(MRK03p);
    c.add(c4);c.add(YLW04pn);c.add(YLW04p);
    c.add(c5);c.add(CRT05pn);c.add(CRT05p);
    c.add(c6);c.add(MNL06pn);c.add(MNL06p);
    c.add(c7);c.add(EGL07pn);c.add(EGL07p);
    c.add(c8);c.add(CPR08pn);c.add(CPR08p);
    c.add(c9);c.add(BPR09pn);c.add(BPR09p);
    c.add(c10);c.add(OPR10pn);c.add(OPR10p);
    c.add(c11);c.add(GPR11pn);c.add(GPR11p);
    c.add(c12);c.add(RLR12pn);c.add(RLR12p);
    c.add(c13);c.add(PRT13pn);c.add(PRT13p);
    c.add(c14);c.add(ERS14pn);c.add(ERS14p);
    c.add(c15);c.add(FLR15pn);c.add(FLR15p);
    c.add(c16);c.add(SCR16pn);c.add(SCR16p);
    c.add(buttonBUY);



} //end of init()


public void actionPerformed (ActionEvent ae) {


    if (ae.getSource() == buttonBUY) {
        second(c);
    }
    if (ae.getSource() == buttonCONFIRM)
     if (Code=="HBW01"){
            Price = 8.00;
            ITname = "HBW Ballpen ";
        }
     else if (Code=="MNG02"){
            Price = 6.00;
            ITname = "Mongol Pencil ";
        }
     else if (Code=="MRK03"){
            Price = 30.00;
            ITname = "Marker";
        }
     else if (Code=="YLW04"){
            Price = 30.00;
            ITname = "Yellow Pad ";
        }
     else if (Code=="CRR05"){
            Price = 10.00;
            ITname = "Cartolina ";
        }
     else if (Code=="MNL06"){
            Price = 6.00;
            ITname = "Manila Paper";
        }
     else if (Code=="EGL07"){
            Price = 12.00;
            ITname = "Elmer’s Glue ";
        }
     else if (Code=="CPR08"){
            Price = 1.00;
            ITname = "Colored Paper ";
        }
     else if (Code=="BPR09"){
            Price = 1.00;
            ITname = "Bond Paper ";
        }
     else if (Code=="OPR10"){
            Price = 1.00;
            ITname = "Oslo Paper ";
        }
     else if (Code=="GPR11"){
            Price = 1.00;
            ITname = "Graphing Paper ";
        }
     else if (Code=="RLR12"){
            Price = 10.00;
            ITname = "Ruler";
        }
     else if (Code=="PTR13"){
            Price = 10.00;
            ITname = "Protractor";
        }
     else if (Code=="ERS14"){
            Price = 5.00;
            ITname = "Eraser ";
        }
     else if (Code=="FLR15"){
            Price = 15.00;
            ITname = "Notebook Filler ";
        }
     else if (Code=="SCR16"){
            Price = 10.00;
            ITname = "Scissor";
        }
     else{
         error(d);
     }
    if (ae.getSource()== back){
        second(c);
    }
    if (ae.getSource() == close){
        System.exit(0);
    }
}



public void second(Container d){
    Label Enter = new Label("Enter Code");
    d.add(Enter);
    d.add(TA); d.add(buttonCONFIRM);
    String Code = TA.getText();
    third(d);







}
public void third(Container e){
    Label Quantity = new Label("Enter  Quantity");
    e.add(Quantity);
    quan = Quantity.getText();
    qty = Integer.parseInt(quan);
    Subtotal = Price * qty;
    Total = Total + Subtotal;
    e.add(buttonCONFIRM1);



}
public void error(Container f){
    Label derror = new Label("Code Not Found");
    f.add(derror);
    f.add(back);

}
public void ask(Container g){
    RP.append(flb1.getText() + "\t" + flb2.getText() + "\t" + flb3.getText() + "\t" + flb4.getText() + "\t  TOTAL");
    RP.append(ITname + "\t" + quan + "\t" + Price + "\t" + Subtotal);

    Label eof = new Label("Would you like to have another transaction? [Y/N]" );
    g.add(eof); g.add(CH);
    choice = CH.getText();
    if (choice == "Y"){
        second(g);
    }
    else 
        last(g);
}
public void last(Container h){
    Label T = new Label ("Total is:");
    String Final = String.valueOf(Total);
    TOT.append(Final);
    h.add(RP);
    h.add(T); h.add(TOT);
    h.add(close);


}
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
JKCM
  • 1
  • 1
  • 1
    as a sidenode to your question, [how to compare strings in java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – SomeJavaGuy Oct 15 '15 at 11:35
  • Yes you can use `CardLayout` with applets, I would, however, highly recommend that you don't use applets or AWT based components, they're kind of out-of-date. Consider using Swing or JavaFX. Have a look at [Creating a GUI With JFC/Swing](http://docs.oracle.com/javase/tutorial/uiswing/) for more details – MadProgrammer Oct 15 '15 at 11:37
  • @SacJn You know what an applet is right? – MadProgrammer Oct 15 '15 at 11:38
  • Your main problem is you don't seem to be adding anything to a container which is displayable on the screen. `c`, you main container, doesn't appear to be initialised, so it's likely to generate a `NullPointerException` – MadProgrammer Oct 15 '15 at 11:40
  • @MadProgrammer I need this program to use applet and AWT, I only have a little experience in java sorry. – JKCM Oct 15 '15 at 11:42
  • 1
    @KevinEsche I thought this can be run without a main method? – JKCM Oct 15 '15 at 11:42
  • @JKCM Then consider gain experience in an API and technology which is actually been used and which has current community experience. – MadProgrammer Oct 15 '15 at 11:44
  • @MadProgrammer how can i initialize it? – JKCM Oct 15 '15 at 11:44
  • @JKCM You create an instance of the class like you do any other object, using the `new` keyword – MadProgrammer Oct 15 '15 at 11:46
  • I recommend against trying to write a program just based on things you picked from Google. If you need to write an applet, you'll need to learn the language properly (enough to know how to compare strings) and learn about applets. Now, AWT applets are ancient, there is a `JApplet` class which allows you to use Swing with an applet. There is an official [tutorial](https://docs.oracle.com/javase/tutorial/uiswing/components/applet.html). – RealSkeptic Oct 15 '15 at 11:46
  • @RealSkeptic I'll study that in the future, but what I need now is to do this as an applet. – JKCM Oct 15 '15 at 11:51
  • Well, then, take the tutorial on AWT applets. But you will need to study, nontheless. – RealSkeptic Oct 15 '15 at 11:54
  • @RealSkeptic yes thanks, had been self-studying the past 3 days on applet. – JKCM Oct 15 '15 at 12:01
  • @MadProgrammer thanks, I did that just now and it initialized, but the applet was empty, uhhm was it because I didn't have a main? – JKCM Oct 15 '15 at 12:02
  • @JKCM Take closer look at the CardLayout tutorial. Your components need to be grouped into "cards", then the "card" needs to be shown. Each "card" can contain only one component, so you will need to use a Container of some type, adding the components you want to appear on each "card" to each Container – MadProgrammer Oct 15 '15 at 20:03

1 Answers1

0

Replace this line :

Container c, d;

With this one :

Container c = new Container(), d = new Container();

Because you're trying to apply a Layout to an unexisting object with a null reference.

For the rest, the applet launches but it printed exceptions in the console. You should keep an eye on it.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89