0

hey there i'm getting NullPointer Exception but I can't find where is the problem

my MainActivity.kt

class MainActivity : AppCompatActivity(), INotesRVAdapter {

lateinit var viewModel: NoteViewModel

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setTheme(R.style.AppTheme)
    setContentView(R.layout.activity_main)

    supportActionBar!!.title = "My Tasks"

    rvTask.layoutManager = LinearLayoutManager(this)
    val adapter  = NotesRVAdapter(this,this)
    rvTask.adapter = adapter

    viewModel = ViewModelProvider(this,ViewModelProvider.AndroidViewModelFactory.getInstance(application)).get(NoteViewModel::class.java)
    viewModel.allTask.observe(this, Observer { list->
        list?.let {
            adapter.updateList(it)
        }
    })
}

override fun onItemClicked(note: Note) {
    viewModel.deleteTask(note)
    Toast.makeText(this , " ${note.text} Task Completed",Toast.LENGTH_SHORT).show()
}

fun submitData(view: View) {
    val noteText = etTask.text.toString()
    if (noteText.isNotEmpty()){
        viewModel.insertTask(Note(noteText))
        Toast.makeText(this , "$noteText Added",Toast.LENGTH_SHORT).show()
    }
}

}

my build.gradle

apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt'

android { compileSdkVersion 32

defaultConfig {
    applicationId "com.example.notesapp"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures {
    viewBinding = true
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
packagingOptions {
    exclude 'META-INF/atomicfu.kotlin_module'
}

kotlinOptions {
    jvmTarget = "1.8"
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

}

dependencies {

var roomVersion = "2.4.2"

implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
implementation "androidx.activity:activity-ktx:$rootProject.activityVersion"


// To use Kotlin annotation processing tool (kapt)
kapt("androidx.room:room-compiler:$roomVersion")


// Dependencies for working with Architecture components
// You'll probably have to update the version numbers in build.gradle (Project)

// Room components
implementation "androidx.room:room-ktx:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
androidTestImplementation "androidx.room:room-testing:$roomVersion"

// Lifecycle components
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"

// Kotlin components
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"

// UI
implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
implementation "com.google.android.material:material:$rootProject.materialVersion"

// Testing
testImplementation "junit:junit:$rootProject.junitVersion"
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
androidTestImplementation ("androidx.test.espresso:espresso-core:$rootProject.espressoVersion", {
    exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"

}

Divyraj
  • 29
  • 4
  • describe all things about your ask and If you don't know about how to ask then refer [this](https://stackoverflow.com/help/how-to-ask) – EAS May 26 '22 at 09:23
  • 2
    Post the logcat as text. There you can see which code causes the exception. No images of logcats please. – blackapps May 26 '22 at 12:36
  • val adapter = NotesRVAdapter(this,this) --> This code looks bit weird, why do you need to pass `this` twice? You can achieve the exact same thing by passing it once. Also please post the logcat messages as text as requested. – Priyabrata May 26 '22 at 15:01

1 Answers1

2

So after reading documentation i found that my room dependency needs to be upgraded and SDK also so after doing this app successfully instaled

Divyraj
  • 29
  • 4