Here is the code that causes java.lang.NullPointerException:
val confidence = SyncReadMem(params.numEntries, UInt(params.confidenceBitsNum.W))
val s0_valid = io.mem.valid
val s0_index = io.mem.bits.pc(params.indxBitsNum-1, 0)
val s1_confidence = RegNext(confidence.read(s0_index, s0_valid))
val s1_inc_confidence = Mux(s1_confidence + 1.U === 0.U, s1_confidence, s1_confidence + 1.U)
val s2_dec_confidence = Mux(s1_confidence === 0.U, s1_confidence, s1_confidence - 1.U)
// val s1_new_confidence = Mux(s1_tag_match && s1_stride_match, s1_confidence + 1.U, s1_confidence - 1.U)
val s1_new_confidence = Mux(s1_tag_match && s1_stride_match, s1_inc_confidence, s2_dec_confidence)
when compile I got error with:
[error] java.lang.NullPointerException
[error] ...
[error] at boom.lsu.IPStridePrefetcherBank.$anonfun$s1_new_confidence$2(IPStirdePrefetcher.scala:82)
[error] at chisel3.internal.prefix$.apply(prefix.scala:49)
[error] at boom.lsu.IPStridePrefetcherBank.$anonfun$s1_new_confidence$1(IPStirdePrefetcher.scala:82)
[error] at chisel3.internal.plugin.package$.autoNameRecursively(package.scala:52)
[error] at boom.lsu.IPStridePrefetcherBank.<init>(IPStirdePrefetcher.scala:82)
[error] at boom.lsu.PrefetchBuffer.$anonfun$banks$2(pre.....
Why does this throw?
Related
I'm trying to use SharedPreference and error occur when i load data. I think the problem is that my data class include MutableList, but i don't know how to handle it.
private lateinit var albumData:MutableList<Album>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadData()
connectAdapter()
btnAddAlbum.setOnClickListener {
if(isStoragePermitted()) {
openGallery()
}
}
}
fun saveData() {
val sharedPreferences = getSharedPreferences("shared preferences", Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
val gson = GsonBuilder().create()
val json = gson.toJson(albumData)
editor.putString("Album data", json)
editor.apply()
}
fun loadData() {
val sharedPreferences = getSharedPreferences("shared preferences", Context.MODE_PRIVATE)
val gson = GsonBuilder().create()
val json = sharedPreferences.getString("Album data", null)
var test = mutableListOf<Album>()
if(json != null) {
Log.d("Log_show", "data load 1")
val type = object : TypeToken<MutableList<Album>>() {}.type
Log.d("Log_show", "data load 2")
albumData = gson.fromJson<MutableList<Album>>(json, type)
Log.d("Log_show", "data load 3")
}else{
albumData = mutableListOf()
}
}
override fun onDestroy() {
saveData()
Log.d("Log_show", "shut down")
super.onDestroy()
}
Above is part of my functions in main activity.
data class Album (
var albumImages : MutableList<Uri>,
var albumTitle : String,
var pictureCount : Int
)
This is my data class "Album"
When i first open the app, it save data well and no error occur.
But when i close the app and open it again, the error occur in process of loading data
This is part of my log of "Log_show" and "data load 3" didn't appear
2020-11-04 23:12:33.418 15720-15720/kr.co.ddophi.autochangingwallpaper D/log: data load 1
2020-11-04 23:12:33.420 15720-15720/kr.co.ddophi.autochangingwallpaper D/log: data load 2
the full log is like this
2020-11-05 14:48:19.319 6956-6956/kr.co.ddophi.autochangingwallpaper D/Log_show: data load 1
2020-11-05 14:48:19.321 6956-6956/kr.co.ddophi.autochangingwallpaper D/Log_show: data load 2
2020-11-05 14:48:19.331 6956-6956/kr.co.ddophi.autochangingwallpaper D/AndroidRuntime: Shutting down VM
2020-11-05 14:48:19.336 6956-6956/kr.co.ddophi.autochangingwallpaper E/AndroidRuntime: FATAL EXCEPTION: main
Process: kr.co.ddophi.autochangingwallpaper, PID: 6956
java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.co.ddophi.autochangingwallpaper/kr.co.ddophi.autochangingwallpaper.MainActivity}: java.lang.RuntimeException: Failed to invoke private android.net.Uri() with no args
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.RuntimeException: Failed to invoke private android.net.Uri() with no args
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:113)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at kr.co.ddophi.autochangingwallpaper.MainActivity.loadData(MainActivity.kt:174)
at kr.co.ddophi.autochangingwallpaper.MainActivity.onCreate(MainActivity.kt:43)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.InstantiationException: Can't instantiate abstract class android.net.Uri
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:110)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:212)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:131)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:222)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:41)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:82)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at com.google.gson.Gson.fromJson(Gson.java:927)
at com.google.gson.Gson.fromJson(Gson.java:892)
at com.google.gson.Gson.fromJson(Gson.java:841)
at kr.co.ddophi.autochangingwallpaper.MainActivity.loadData(MainActivity.kt:174)
at kr.co.ddophi.autochangingwallpaper.MainActivity.onCreate(MainActivity.kt:43)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Any help would be appreciated
Thank you
You should custom TypeAdapter for serialize and deserialize Uri.
class UriTypeAdapter : TypeAdapter<Uri>() {
override fun write(out: JsonWriter?, value: Uri?) {
value?.let {
out?.value(it.toString())
} ?: run {
out?.nullValue()
}
}
override fun read(reader: JsonReader?): Uri {
return if (reader?.peek() == JsonToken.NULL) {
reader.nextNull()
Uri.EMPTY
} else {
val uriString = reader?.nextString()
uriString?.let {
Uri.parse(it)
} ?: run {
Uri.EMPTY
}
}
}
}
and setup Gson like this:
val gson = GsonBuilder()
.registerTypeHierarchyAdapter(Uri::class.java, UriTypeAdapter())
.create()
I'm running spark job with kafka-clients:2.2.1 and
spark-streaming-kafka-0-10_2.11:2.4.3
The job is running in the following mode:
val scc: StreamingContext = new StreamingContext(spark, "180 seconds")
val kafkaSink: Broadcast[KafkaSink] = ...
val stream = KafkaUtils.createDirectStream(...)
stream //. do some logic
.foreachRDD { (rdd, time) =>
p.foreach { row =>
val rowJson = convertRowToJSON(row)
kafkaSink.value.send(kafkaOptions.topic, rowJson) }
}
The job is running but for some mini-batches, there are failed tasks with following exceptions:
java.lang.NullPointerException
at org.apache.kafka.clients.producer.KafkaProducer.waitOnMetadata(KafkaProducer.java:1010)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:916)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:897)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:774)
at com.outbrain.recs.rtap.kafka.KafkaSink.send(KafkaSink.scala:42)
at
What could be the reason for those exceptions?
I am trying to use withFixture method to initialize my var ip2GeoTestJson and use it throughout my tests. I was able to achieve the desired logic with var year. I believe the error I am getting (parsing JNothing) is caused because the withFixture is not initializing my ip2GeoTestJson with the JSON.
I am currently getting this error:
*** RUN ABORTED ***
An exception or error caused a run to abort: java.lang.ClassCastException was thrown scenario("event.client_ip_address and event_header.client_ip_address both have values") -, construction cannot continue: "org.json4s.JsonAST$JNothing$ cannot be cast to org.json4s.JsonAST$JObject" (IP2GeoTestSuite.scala:51)
Code:
class IP2GeoTestSuite extends FeatureSpec with SparkContextFixture {
var ip2GeoTestJson: JValue = null
var year: String = null
feature("feature") {
scenario("scenario") {
println(ip2GeoTestJson)
assert(year != null)
assert(ip2GeoTestJson != null)
}
}
def withFixture(test: NoArgTest): org.scalatest.Outcome = {
year = test.configMap("year").asInstanceOf[String]
val ip2GeoConfigFile = test.configMap("config").asInstanceOf[String]
val ip2GeoUrl = getClass.getResourceAsStream(s"/$ip2GeoConfigFile")
val ip2GeoJsonString = Source.fromInputStream(ip2GeoUrl).getLines.mkString("")
System.out.println(ip2GeoJsonString)
ip2GeoTestJson = parse(ip2GeoJsonString)
try {
test()
}
}
}
The code works fine when the lines regarding ip2GeoData are moved to the top of the class like so however I need to hardcode the file name:
class IP2GeoTestSuite extends FeatureSpec with SparkContextFixture {
val ip2GeoConfigFile = "ip2geofile.json"
val ip2GeoUrl = getClass.getResourceAsStream(s"/$ip2GeoConfigFile")
val ip2GeoJsonString = Source.fromInputStream(ip2GeoUrl).getLines.mkString("")
System.out.println(ip2GeoJsonString)
val ip2GeoTestJson = parse(ip2GeoJsonString)
var year: String = null
feature("feature") {
scenario("scenario") {
println(ip2GeoTestJson)
assert(year != null)
assert(ip2GeoTestJson != null)
}
}
def withFixture(test: NoArgTest): org.scalatest.Outcome = {
year = test.configMap("year").asInstanceOf[String]
try {
test()
}
}
}
Set params before every test (see http://www.scalatest.org/user_guide/sharing_fixtures#withFixtureOneArgTest):
case class FixtureParams(year: String, ip2GeoTestJson: JValue)
class IP2GeoTestSuite extends FeatureSpec with SparkContextFixture {
feature("feature") {
scenario("scenario") {
println(ip2GeoTestJson)
assert(year != null)
assert(ip2GeoTestJson != null)
}
}
override def withFixture(test: OneArgTest): org.scalatest.Outcome = {
val year = test.configMap("year").asInstanceOf[String]
val ip2GeoConfigFile = test.configMap("config").asInstanceOf[String]
val ip2GeoUrl = getClass.getResourceAsStream(s"/$ip2GeoConfigFile")
val ip2GeoJsonString = Source.fromInputStream(ip2GeoUrl).getLines.mkString("")
val fixtureParam = FixtureParam(year, parseJson(ip2GeoJsonString))
try {
withFixture(test.toNoArgTest(fixtureParam))
} finally {
// Close resourses to avoid memory leak and unpredictable behaviour
ip2GeoUrl.close()
}
}
}
Set params only once before any test will run (http://www.scalatest.org/user_guide/sharing_fixtures#beforeAndAfter):
class IP2GeoTestSuite extends FeatureSpec with BeforeAndAfter {
var ip2GeoTestJson: JValue = null
var year: String = null
before {
// Load config manually because configMap isn't available here.
val config = ConfigFactory.load()
year = config.getString("year")
val ip2GeoConfigFile = "ip2geofile.json"
val ip2GeoUrl = getClass.getResourceAsStream(s"/$ip2GeoConfigFile")
val ip2GeoJsonString = Source.fromInputStream(ip2GeoUrl).getLines.mkString("")
ip2GeoUrl.close()
System.out.println(ip2GeoJsonString)
ip2GeoTestJson = parseJson(ip2GeoJsonString)
}
feature("feature") {
scenario("scenario") {
println(ip2GeoTestJson)
assert(year != null)
assert(ip2GeoTestJson != null)
}
}
}
I'm trying to make an insert into a MySQL table and to return the row with the auto increment id. My code is below:
private val Log = TableQuery[GCMLogTable]
def save(log: GCMLog): Try[GCMLog] = Try {
val newId = (Log returning Log.map(_.id)) += log
log.copy(id = newId)
}
But my compilation fails for my code with the below error:
type mismatch;
found : slick.profile.FixedSqlAction[Long,slick.dbio.NoStream,slick.dbio.Effect.Write]
required: Long
Also tried
def save(log: GCMLog): Try[GCMLog] = Try {
(Log returning Log.map(_.id)
into ((log, newId) => log.copy(id = newId))
) += log
}
But still fails with
type mismatch;
found : slick.profile.FixedSqlAction[models.GCMLog,slick.dbio.NoStream,slick.dbio.Effect.Write]
required: models.GCMLog
[I referred the SO question How to catch slick postgres exceptions for duplicate key value violations and Slick documentation here http://slick.typesafe.com/doc/3.1.1/queries.html ]
Much appreciated if someone can tell me what's going on and how this can be fixed.
Thanks!
def save(log: GCMLog): Try[GCMLog] = Try {
(Log returning Log.map(_.id))
into ((log, newId) => log.copy(id = newId))
) += log
}
UPDATE:
It looks db.run needs to be perform to convert that Action into result.
slick supports it:
def create(objectToCreate: MyCaseClass): MyCaseClass = {
db.withSession {
(self returning self) += objectToCreate
}
}
Try this
private val Log = TableQuery[GCMLogTable]
private val db = Database.forConfig("mysql")
def save(log: GCMLog): Try[GCMLog] = Try {
val newId = db.run((Log returning Log.map(_.id) into ((Log,id) => Log.copy(id=id))) += log)
Await.result(newId, Duration.Inf)
}
I have a selectable feature which is not normally required. However to support this feature, some I/O ports should be added to the origin Module I/O port.
I am doing it in this way:
import Chisel._
class TestModule extends Module {
class IOBundle extends Bundle {
val i = Bool(INPUT)
val o = Bool(OUTPUT)
}
class IOBundle_EXT extends IOBundle {
val o_ext = Bool(OUTPUT)
}
val io = if(true) new IOBundle_EXT else new IOBundle;
io.o := io.i
io.o_ext := io.i
}
After running sbt "run TestModule --backend c --compile --test --genHarness", the compiler complains:
[error] xxxx/test/condi_port.scala:17: value o_ext is not a member of TestModule.this.IOBundle
[error] io.o_ext := io.i
[error] ^
[error] one error found
[error] (compile:compile) Compilation failed
So the if statement has no effect. val io is still assigned to IOBundle, rather than the extended IOBoundle_EXT, which makes no sense to me.
Chisel now supports Options in IO bundles.
As an example, I explored Options here (https://github.com/ucb-bar/riscv-boom/commit/da6edcb4b7bec341e31a55567ee04c8a1431d659), but here's a summary:
class MyBundle extends Bundle
{
val my_ext = if (SOME_SWITCH) Some(ExtBundle) else None
}
...
io.my_ext match
{
case Some(b: ExtBundle) =>
my_ext.a := Bool(false)
...
case _ => require (!SOME_SWITCH)
}
It's incredibly verbose, but I was able to get it working even when doing bulk connects and hiding bundles within bundles, etc.
Even though the compiler could determine that only one result is possible (the expression is always true), the type system sets the type of the result equal to the greatest common subtype of the two possible (true or false) sub-expressions.
You can verify this trivially with the following:
scala> val result = if (true) 1 else "one"
result: Any = 1
Try this:
import Chisel._
class TestModule(val useEXT : Boolean) extends Module {
class IOBundle extends Bundle {
val i = Bool(INPUT)
val o = Bool(OUTPUT)
}
class IOBundle_EXT extends IOBundle {
val o_ext = Bool(OUTPUT)
}
val io = {
if(useEXT) {
val res = new IOBundle_EXT; res.o_ext := res.i; res
} else {
new IOBundle }};
io.o := io.i
}