I have the following code in my flink project:
public class Test {
public static void main(String[] args) throws Exception {
// set up the execution environment
final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
DataSet<Event> events =
env.readCsvFile(args[0]).pojoType(
Event.class,
"time",
"vid",
"speed",
"xWay",
"lane",
"dir",
"seg",
"pos"
);
System.out.println("----> " + events.count());
}
}
And this is the class Event:
class Event {
public int time;
public int vid;
public int speed;
public int xWay;
public int lane;
public int dir;
public int seg;
public int pos;
public Event() { }
public Event(int time_in, int vid_in, int speed_in, int xWay_in, int lane_in, int dir_in, int seg_in, int pos_in) {
this.time = time_in;
this.vid = vid_in;
this.speed = speed_in;
this.xWay = xWay_in;
this.lane = lane_in;
this.dir = dir_in;
this.seg = seg_in;
this.pos = pos_in;
}
}
The project compiles but when I run it, there is an error:
java.lang.ClassCastException: org.apache.flink.api.java.typeutils.GenericTypeInfo cannot be cast to org.apache.flink.api.java.typeutils.PojoTypeInfo
The CSV file has 8 integer values separated by a comma in each line.
The documentation has the following example:
DataSet<Person>> csvInput = env.readCsvFile("hdfs:///the/CSV/file")
.pojoType(Person.class, "name", "age", "zipcode");
I don't know if the POJO definition is wrong, surely it is. I achieved what I wanted using map and readTextFile but this could be more expensive.
The ClassCastException is a bug that will be fixed soon and replaced by a more meaningful exception. Event is a GenericType instead of a PojoType. I think the reason might be that Event is member class instead of a global accessible class. Adding the static modifier should solve the problem.
Related
My current problem is that I am assigned to created a program that should within the private fields assign tasks[] an array of task. Then within the constructor, that creates the task[] array, giving it the capacity of INITIAL_CAPAITY, and setting numTasks to zero.
I am new and confused on I can tackle this problem
I have tried declaring it within the constructor but there has been no luck.
Task.java
public class Task {
private String name;
private int priority;
private int estMinsToComplete;
public Task(String name, int priority, int estMinsToComplete) {
this.name=name;
this.priority=priority;
this.estMinsToComplete = estMinsToComplete;
}
public String getName() {
return name;
}
public int getPriority() {
return priority;
}
public int getEstMinsToComplete() {
return estMinsToComplete;
}
public void setName(String name) {
this.name = name;
}
public void setEstMinsToComplete(int newestMinsToComplete) {
this.estMinsToComplete = newestMinsToComplete;
}
public String toString() {
return name+","+priority+","+estMinsToComplete;
}
public void increasePriority(int amount) {
if(amount>0) {
this.priority+=amount;
}
}
public void decreasePriority(int amount) {
if (amount>priority) {
this.priority=0;
}
else {
this.priority-=amount;
}
}
}
HoneyDoList.java
public class HoneyDoList extends Task{
private String[] tasks;
//this issue to my knowledge is the line of code above this
private int numTasks;
private int INITIAL_CAPACITY = 5;
public HoneyDoList(String tasks, int numTasks, int INITIAL_CAPACITY,int estMinsToComplete, String name,int priority) {
super(name,priority,estMinsToComplete);
numTasks = 0;
tasks = new String[]{name,priority,estMinsToComplete};
//as well as here^^^^^^^^
}
My expected result is to be able to print out the list through honeydo class. I need to manipulate the code a bit more after adding a few other methods.
Your problem is that your constructor parameter tasks has the same name as that field of your class.
So you assign to the method parameter in your constructor, not to the field. And luckily those two different "tasks" entities have different types, otherwise you would not even notice that something is wrong.
Solution: use
this.tasks = new String...
within the body of the constructor!
And the real answer: you have to pay a lot attention to such subtle details. And by using different names for different things you avoid a whole class of issues!
Also note: it sounds a bit strange that a class named Task contains a list of tasks, which are then strings. The overall design is a bit weird...
I just started to learn java and am not fimiliar with the language. this is an online assignment i am doing for fun and to get more fimiliar, and can't figure out the multiple errors i am getting with the constructor line. Please help
public class WhackAMole {
public static void main(String[] args) {
int score;
int molesLeft;
int attemptsLeft;
char [][]moleGrid=new char[10][10];
int numAttempts; //is this needed
int gridDimensions; // is this also needed
/*Multiple markers at this line
- Syntax error on token "int", delete this token
- Syntax error, insert ";" to complete Statement
- Syntax error on token "int", delete this token
- numAttempts cannot be resolved to a variable
- gridDimensions cannot be resolved to a variable
- Syntax error on token "int", delete this token
- The method WhackAMole(int, int) is undefined for the type
WhackAMole*/
WhackAMole(int numAttempts, int gridDimensions) {
this.numAttempts=numAttempts ; //error-cannot use this in static content
this.gridDimensions=gridDimensions ; // error-cannot use this in static content
}
}
}
Move your constructor out of main() method.
I reccomend you to do some basic beginner level java tutorial. You cannot put the constructor in another method (it's in the main method). Also to use this.numAttempts you need object Attributes. I tried to move the code-snippets to give it more sense:
public class WhackAMole {
// Those are attributes
private int score;
private int molesLeft;
private int attemptsLeft;
private char[][] moleGrid = new char[10][10];
private int numAttempts; // is this needed
private int gridDimensions; // is this also needed
// Constructor
public WhackAMole(int numAttempts, int gridDimensions) {
this.numAttempts = numAttempts;
this.gridDimensions = gridDimensions;
}
public void play() {
// Game logic here
}
/* This Method should propably be in another class */
public static void main(String[] args) {
final WhackAMole wham = new WhackAMole(42, 1234567);
wham.play();
}
}
You were definining method inside a method which is not allowed in java. Also, I have moved the attributes to class level.
Please use below code:
public class WhackAMole {
int score;
int molesLeft;
int attemptsLeft;
char[][] moleGrid = new char[10][10];
int numAttempts; //is this needed
int gridDimensions; // is this also needed
WhackAMole(final int numAttempts, final int gridDimensions) {
this.numAttempts = numAttempts; //error-cannot use this in static content
this.gridDimensions = gridDimensions; // error-cannot use this in static content
}
public static void main(final String[] args) {
WhackAMole whackAMole = new WhackAMole(30, 40);
System.out.println("numAttempts:" + whackAMole.numAttempts + " gridDimensions:" + whackAMole.gridDimensions);
}
}
I am trying to create a class that will create matrices by taking an array as an input (among other things). This array will be assigned to a record (Record9). However I am getting this error when trying to compile. You can find my code below:
public class Matrix3x3flat {
private class Record9 {
public long r1c1;
public long r1c2;
public long r1c3;
public long r2c1;
public long r2c2;
public long r2c3;
public long r3c1;
public long r3c2;
public long r3c3;
}
private Record9 mat;
public Record9(long[] arr) {
Record9 this.mat = new Record9();
this.mat.r1c1 = arr[0];
this.mat.r1c2 = arr[1];
this.mat.r1c3 = arr[2];
this.mat.r2c1 = arr[3];
this.mat.r2c2 = arr[4];
this.mat.r2c3 = arr[5];
this.mat.r3c1 = arr[6];
this.mat.r3c2 = arr[7];
this.mat.r3c3 = arr[8];
return this.mat;
}
}
I don't understand the problem but I do suspect it has something to do with me not properly referencing this.mat in the return statement.
Well, few things I notice. Edit: as mentioned below, constructor name needs to be the same as class name.
2) Why are you re declaring mat as a type of Record9. You already set it up top as a type of Record9, no need to define it again, you can just say this.mat = whatever it needs to be
My idea is you want to create instance of Record9 on public Record9(long[] arr), currently you use return statement i side of constructor it is not allow. so you need to convert that to a method.
try like this :
public class Matrix3x3flat {
private class Record9 {
public long r1c1;
public long r1c2;
public long r1c3;
public long r2c1;
public long r2c2;
public long r2c3;
public long r3c1;
public long r3c2;
public long r3c3;
}
private Record9 mat;
public Record9 instance(long[] arr) {
this.mat = new Record9();
this.mat.r1c1 = arr[0];
this.mat.r1c2 = arr[1];
this.mat.r1c3 = arr[2];
this.mat.r2c1 = arr[3];
this.mat.r2c2 = arr[4];
this.mat.r2c3 = arr[5];
this.mat.r3c1 = arr[6];
this.mat.r3c2 = arr[7];
this.mat.r3c3 = arr[8];
return this.mat;
}
}
I am getting an IndexOutOfBoundsExpetion when reading a string from a flatbuffer for some reason.
my schema:
namespace com.busalarmclock.flatbuffers;
table Message {
routes:[Route];
stops:[Stop];
trips:[Trip];
}
table Route {
route_id:string;
route_name:string;
route_description:string;
trips:[Trip];
}
table Trip {
trip_id:string;
op_days:int;
stops:[TripStop];
}
table Stop {
stop_id:int;
stop_name:string;
stop_lon:double;
stop_lat:double;
}
table TripStop {
stop:Stop;
arrival_time:long;
departure_time:long;
dropoff_type:short;
}
root_type Message;
this is how I'm writing my buffer:
public static byte[] createStopMessage(TopDocs hits, IndexSearcher indexSearcher) throws IOException {
FlatBufferBuilder builder = new FlatBufferBuilder(1);
int[] stopData = new int[hits.totalHits];
for (int i = 0; i < hits.totalHits; i++)
stopData[i] = createStopObject(indexSearcher.doc(hits.scoreDocs[i].doc), builder);
int stopsOffset = Message.createStopsVector(builder, stopData);
Message.startMessage(builder);
Message.addStops(builder, stopsOffset);
int root = Message.endMessage(builder);
builder.finish(root);
return builder.sizedByteArray();
}
public static byte[] createTripStopsMessage(TripModel trip, IndexSearcher indexSearcher) {
FlatBufferBuilder builder = new FlatBufferBuilder(1);
int[] tripStopData = new int[trip.tripStopModels.length];
for (int i = 0; i < trip.tripStopModels.length; i++)
tripStopData[i] = createTripStopObject(trip.tripStopModels[i], builder);
System.out.printf("tripId:%s", trip.tripId);
int tripIdOffset = builder.createString(trip.tripId);
int tripStopsOffset = Trip.createStopsVector(builder, tripStopData);
Trip.startTrip(builder);
Trip.addTripId(builder, tripIdOffset);
Trip.addStops(builder, tripStopsOffset);
int tripOffset = Trip.endTrip(builder);
Message.startMessage(builder);
Message.addTrips(builder, tripOffset);
int messageOffset = Message.endMessage(builder);
builder.finish(messageOffset);
return builder.sizedByteArray();
}
public static int createTripStopObject(TripStopModel tripStopModel, FlatBufferBuilder builder) {
int stopOffset = createStopObject(tripStopModel.stop, builder);
return TripStop.createTripStop(builder, stopOffset, tripStopModel.arrivalTime,
tripStopModel.departureTime, tripStopModel.dropoffType);
}
and these are my models:
public class TripModel {
public String tripId;
public int opDays;
public TripStopModel[] tripStopModels;
public TripModel() {
}
public TripModel(String tripId) {
this.tripId = tripId;
}
public TripModel(String tripId, TripStopModel[] tripStationHits) {
this.tripStopModels = tripStationHits;
this.tripId = tripId;
}
public TripModel(String tripId, int opDays, TripStopModel[] tripStationHits) {
this.tripId = tripId;
this.opDays = opDays;
this.tripStopModels = tripStationHits;
}
import org.apache.lucene.document.Document;
/**
* Created by User on 09/07/2016.
*/
public class TripStopModel {
public long arrivalTime;
public long departureTime;
public short dropoffType;
public Document stop;
public TripStopModel() {
}
public TripStopModel(long arrivalTime, long departureTime, short dropoffType, Document stop) {
this.arrivalTime = arrivalTime;
this.departureTime = departureTime;
this.dropoffType = dropoffType;
this.stop = stop;
}
}
I have a lucene database, and I am trying to get some data from it to a flatbuffer message.
when creating the buffer I get no errors, but I get an IndexOutOfBoundsExeption when reading the buffer, from the first one.
I checked, and the String is not null when parsing.
There doesn't appear anything wrong with how you you create the buffer.
If you get an IndexOutOfBoundsException, that typically means the buffer got corrupted between when it got created and read. Can you check just before you read the buffer, that the size and the bytes it contains are the same compared to when you just created it? Are you sure you are using a binary file or transfer protocol?
fixed it :)
I was adding the tripOffset as the tripVector offset by mistake!
For what it's worth, I got a similar error when my ByteBuffer was too small. The write seemingly went fine, but, in reality, it got truncated. The read threw an IOOBE. When I increased the size of the buffer, everything worked fine.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
getting a frustrating null exception error when the class junction is being used in functions. The class should be initialised so this shouldn't be an issue. Help ;_;
class RobotData {
class Junction {
public int juncX;
public int juncY;
public int arrivalHeading;
}
private static int maxJunctions = 10000; //Max junctions likely to occur
private static int junctionCounter;
private static Junction[] junction;
RobotData() {
junctionCounter = 0;
junction = new Junction[maxJunctions];
}
public void resetJunctionCounter() { junctionCounter = 0; }
public void recordJunction(IRobot robot) {
junction[junctionCounter].juncX = robot.getLocation().x;
junction[junctionCounter].juncY = robot.getLocation().y;
junction[junctionCounter].arrivalHeading = robot.getHeading();
junctionCounter++;
}
public void printJunction() {
System.out.println(junction[junctionCounter].juncX);
System.out.println(junction[junctionCounter].juncY);
System.out.println(junction[junctionCounter].arrivalHeading);
}
}
The class RobotData is being initialised properly, but when the functions are being called I get the null error indicating that junction[junctionCounter] hasn't been initialised yet. Unsure why (obviously) as it should be initialised when RobotData is.
When you write this line:
junction = new Junction[maxJunctions];
You create an array of maxJunctions references to Junction instances on the heap. All of them are null until you point them to an object on the heap by calling new.
Here's another idea:
public class Junction {
public final int juncX;
public final int juncY;
public final int arrivalHeading;
public String toString() {
return String.format("x = %d y = %d arrivalHeader = %d", juncX, juncY, arrivalHeading);
}
}
public class RobotData {
private List<Junction> junctions;
RobotData() {
this.junctions = new ArrayList<Junction>();
}
public void recordJunction(IRobot robot) {
Junction junction = new Junction();
junction.juncX = robot.getLocation().x;
junction.juncY = robot.getLocation().y;
junction.arrivalHeading = robot.getHeading();
junctions.add(junction);
}
}