How to fix missing F on Java [duplicate] - java

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 8 months ago.
I try to make a game Flappy Bird and want to test my result on DesktopLauncher, but something is happening that makes two errors. I can't find any F on the list.
package com.mygdx.flappy;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
public class DesktopLauncher {
public static void main (String[] arg) {
Lwjgl3ApplicationConfiguration config = new Lwjgl3ApplicationConfiguration();
config.width = 480; //this is the error part
config.heigth = 800; //this is the error part
new Lwjgl3Application(new MainFlappyBird(), config);
}
}
here's the picture

Config is an object of type „Lwjgl3ApplicationConfiguration“ And this type does not have properties „width“ and „height“. That‘s why you can‘t assign values to this property, because it does not exist.

Related

null pointer exception when I check if a blob exists [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 9 months ago.
I have a com.google.cloud.storage.Blob object. I have been able to download and upload just fine with this object in my java code, but I wanted to check if something exists before starting a process.In a nutshell here is the part of my code giving me issues.
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.BlobId;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
public class Function implements BackgroundFunction<GcsEvent> {
public void accept(GcsEvent event, Context context) {
String bucketname = "my_bucket";
String blob_path = "path/to/my/object.jpg";
Storage storage = StorageOptions.newBuilder().setProjectId("my_project_id").build().getService();
Blob b = storage.get(BlobId.of(bucketname, blob_path));
// this is where the null pointer exception occurs
boolean exists = b.exists();
if (exists) {
//do something
}
}
}
Eclipse doesn't seem to catch it in the IDE. and the examples seem to follow this layout as well. Would anyone know what is going on?
I am running on a Linux environment machine. (it failed in cloud function as well).
I am using the Java 11 runtime.
I feel silly. I found out that storage.get(BlobId.of(bucketname, blob_path)); will return null if it can't find the object. instead of using b.exists(), one should check if it is null with if (b != null) { //do stuff }.

Learning Java classes and methods, getting this error while testing my code [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 1 year ago.
I am following along with JAVA book for CS101 and trying to wrap my head around this exercise, but when I run my code, I keep getting this error
Error: Could not find or load main class Riddle
Caused by: java.lang.ClassNotFoundException: Riddle
JAVA CODE
public class Riddle{
private String question; //instance variables
private String answer;
public Riddle( String q, String a) //constructor
{
question = q;
answer = a;
}
public String getQuestion() //instance method
{
return question;
}
public String getAnswer()
{
return answer;
}
}
public class RiddleUser{
public static void main ( String argv [] ){
Riddle riddle1= new Riddle (
"What is black and white and red all over?",
"An embarrassed zebra.");
Riddle riddle2= new Riddle (
"What is black and white and read all over?",
"A newspaper." );
System.out.println("Here are two riddles:");
System.out.println(riddle1.getQuestion());
System.out.println(riddle2.getQuestion());
System.out.println("The answer to the first riddle is:");
System.out.println(riddle1.getAnswer());
System.out.println("The answer to the second riddle is:");
System.out.println(riddle2.getAnswer());
}
}
Here is the exercise I am doing, I also uploaded the PDF of the book, the exercise is on pages 71-73.
Basically, it wants to show you how to write a class and test it, If I am getting this correctly.
enter image description here
enter image description here
enter link description here
On a Java file, only 1 class can be public, the public class is the one with the main method.
The file and the public class must have the same name, RiddleUser.java in this case.
After declaring RiddleUser class in its own file.
This should work if you add private scope modifier to the RiddleUser class, without declaring it in its own file aswell.
Say your source path folder is ~/riddle-project/src/
riddle-project
└── src
├── Riddle.java
└── RiddleUser.java
Execute the following:
# compile main class
$ javac src/Riddle.java
# run main class with specified class path
$ java -classpath ./src/ Riddle
Class Path is the directory that contains the .class compiled java files

Java - Calling objects from other classes? [duplicate]

This question already has answers here:
What does a "Cannot find symbol" or "Cannot resolve symbol" error mean?
(18 answers)
Closed 5 years ago.
I'm making a DnD program to essentially just keep track of everything. I have three classes relevant to this problem, my main (which is just a hub to execute everything else), an Item class which holds all the properties of various individual items, and an InventoryList class which basically just defines an unholy load of items(this is a separate class because of the sheer number of items in 4e DnD). I had decided on making an Array of these Items, and calling that the users inventory; first off, is there a better way to do that -- i.e. using a HashMap or ArrayList? Second, I'm getting a classic "error, cannot find symbol" compiler error in the following code (shortened to relevance):
Main Method:
public class DnD {
public static void main (String[] args) throws FileNotFoundException, IOException {
. . .
Item[] myInventory = {Candle}; //error: symbol cannot be found
} //main()
} //DnD
Item Method:
public class Item {
. . .
public static Item Candle = new Item("Candle", 1, "Provides dim light for 2 squares, and lasts for 1 hour.");
public Item(String _name, double _price, String _desc) {
name = _name;
price = priceConverter(_price);
description = _desc;
} //Item()
} //Item
InventoryList Method:
public class InventoryList {
. . .
public InventoryList() {
// I have no idea where to proceed with this.
// Is there a way for me to use this to initialize ...
// ... an Array of Items in this method, to use in the main?
// The Item object stores name, price, and a description; ...
// ... is there a way to create an Array or similar to ...
// ... display all that information at once and hold it together?
}
}
I seem to be unable to summon Items in the main from the Item class. My Inventory (Item[]) is not working because of this.
For the "cannot find symbol" error, you can fix it like this:
Item[] myInventory = {Item.Candle};
Candle is defined in the class Item, so you have to type the class name out as well.
For how to implement InventoryList, I recommend using an ArrayList<Item> to store the player's items:
private ArrayList<Item> items = new ArrayList<>();
public ArrayList<Item> getItems() {
return items;
}
ArrayLists can dynamically change size, so if you want to add more items to the player's inventory, you can do that very conveniently.
You must qualify the static property
Item[] myInventory = {Item.Candle};
Second question is rather braod, do more research and ask a more focussed question.

Define simple java class, then error while writing(Permission denied) [duplicate]

This question already has an answer here:
How to bypass permission denied error?
(1 answer)
Closed 5 years ago.
I just begin to learn Java, and write a simple java code to define class
import java.util.*;
class People {
People() {
System.out.println("New people");
}
}
public class Test {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
new People();
}
}
}
Then I use javac to compile it, and some error happened
Test.java:3: error: error while writing People: People.class (Permission denied)
class People {
^
1 error
How can I solve it?
error while writing People: People.class (Permission denied)
It has no relation with java code.
You execute the javac command with a user that has not the rights to create a file (the generated .class) where you try to do it.
Just check the rights of the user and the folder where you generate the .class files and adjust the one or the other.

Java - metode inside a metode syntax issues [duplicate]

This question already has answers here:
Does Java support inner / local / sub methods?
(5 answers)
Closed 5 years ago.
Really don't know what it's called so I'm having a hard time searching for the answer.
Anyhow, I want to make a metode with metode inside (if that's even possible?).
public void log() {
public makeLogElement() {
//making a logelement to write inn
}
public write(String text) {
logelement.setText(logelement.getText() + text);
}
}
log myLog = new log();
myLog.makeLogElement();
myLog.write("This'll be written in the log");
What is the right syntax for making something like this?
It's not possible. But you can create a class inside a method.

Categories